Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Merge intervals leetcode
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
class Solution { public: vector<vector<int>> merge(vector<vector<int>>& intervals) { int n=intervals.size(); vector<vector<int>>ans; if(n==0 ) { return ans; } if(n==1) { return intervals; } sort(intervals.begin(),intervals.end()); int s=intervals[0][0]; int e=intervals[0][1]; for(int i=1;i<n;i++) { int a=intervals[i][0]; int b=intervals[i][1]; if(a>=s && a<=e) { s=min(s,a); e=max(b,e); } else { vector<int>v; v.push_back(s); v.push_back(e); s=a; e=b; ans.push_back(v); } } vector<int>v; v.push_back(s); v.push_back(e); ans.push_back(v); return ans; } };
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
edit mode
|
history
|
discussion