41
41
42
42
<!-- 这里可写通用的实现逻辑 -->
43
43
44
+ ** 方法一:区间合并**
45
+
44
46
区间合并,将所有存在交集的区间进行合并。
45
47
46
48
模板:
47
49
48
- ``` py
50
+ ``` python
49
51
def merge (intervals ):
50
- res = []
51
- intervals.sort(key = lambda x : x[ 0 ] )
52
- st = ed = - 1
53
- for s, e in intervals:
52
+ ans = []
53
+ intervals.sort()
54
+ st, ed = intervals[ 0 ]
55
+ for s, e in intervals[ 1 :] :
54
56
if ed < s:
55
- if st != - 1 :
56
- res.append([st, ed])
57
- st, ed = e[0 ], e[1 ]
57
+ ans.append([st, ed])
58
+ st, ed = s, e
58
59
else :
59
- ed = max (ed, e[1 ])
60
- if st != - 1 :
61
- res.append([st, ed])
62
- return res
60
+ ed = max (ed, e)
61
+ ans.append([st, ed])
62
+ return ans
63
63
```
64
64
65
65
<!-- tabs:start -->
@@ -71,19 +71,17 @@ def merge(intervals):
71
71
``` python
72
72
class Solution :
73
73
def merge (self , intervals : List[List[int ]]) -> List[List[int ]]:
74
- intervals.sort(key = lambda x : x[ 0 ] )
75
- st = ed = - 1
76
- res = [ ]
77
- for s, e in intervals:
74
+ intervals.sort()
75
+ ans = []
76
+ st, ed = intervals[ 0 ]
77
+ for s, e in intervals[ 1 :] :
78
78
if ed < s:
79
- if st != - 1 :
80
- res.append([st, ed])
79
+ ans.append([st, ed])
81
80
st, ed = s, e
82
81
else :
83
82
ed = max (ed, e)
84
- if st != - 1 :
85
- res.append([st, ed])
86
- return res
83
+ ans.append([st, ed])
84
+ return ans
87
85
```
88
86
89
87
### ** Java**
@@ -94,23 +92,20 @@ class Solution:
94
92
class Solution {
95
93
public int [][] merge (int [][] intervals ) {
96
94
Arrays . sort(intervals, Comparator . comparingInt(a - > a[0 ]));
97
- int st = - 1 , ed = - 1 ;
98
- List<int[]> res = new ArrayList<> ();
99
- for (int [] e : intervals) {
100
- if (ed < e[0 ]) {
101
- if (st != - 1 ) {
102
- res. add(new int []{st, ed});
103
- }
104
- st = e[0 ];
105
- ed = e[1 ];
95
+ int st = intervals[0 ][0 ], ed = intervals[0 ][1 ];
96
+ List<int[]> ans = new ArrayList<> ();
97
+ for (int i = 1 ; i < intervals. length; ++ i) {
98
+ int s = intervals[i][0 ], e = intervals[i][1 ];
99
+ if (ed < s) {
100
+ ans. add(new int []{st, ed});
101
+ st = s;
102
+ ed = e;
106
103
} else {
107
- ed = Math . max(ed, e[ 1 ] );
104
+ ed = Math . max(ed, e);
108
105
}
109
106
}
110
- if (st != - 1 ) {
111
- res. add(new int []{st, ed});
112
- }
113
- return res. toArray(new int [res. size()][]);
107
+ ans. add(new int []{st, ed});
108
+ return ans. toArray(new int [ans. size()][]);
114
109
}
115
110
}
116
111
```
@@ -120,31 +115,22 @@ class Solution {
120
115
``` cpp
121
116
class Solution {
122
117
public:
123
- vector<vector<int >> merge(vector<vector<int >> & intervals) {
118
+ vector<vector<int >> merge(vector<vector<int >>& intervals) {
124
119
sort(intervals.begin(), intervals.end());
125
- vector<vector< int >> res ;
126
- int st = -1, ed = -1 ;
127
- for (auto e : intervals)
120
+ int st = intervals [ 0 ] [ 0 ] , ed = intervals [ 0 ] [ 1 ] ;
121
+ vector<vector< int >> ans ;
122
+ for (int i = 1; i < intervals.size(); ++i )
128
123
{
129
- if (ed < e[ 0] )
130
- {
131
- if (st != -1)
132
- {
133
- res.push_back({st, ed});
134
- }
135
- st = e[ 0] ;
136
- ed = e[ 1] ;
137
- }
138
- else
124
+ int s = intervals[ i] [ 0 ] , e = intervals[ i] [ 1 ] ;
125
+ if (ed < s)
139
126
{
140
- ed = max(ed, e[ 1] );
127
+ ans.push_back({st, ed});
128
+ st = s, ed = e;
141
129
}
130
+ else ed = max(ed, e);
142
131
}
143
- if (st != -1)
144
- {
145
- res.push_back({st, ed});
146
- }
147
- return res;
132
+ ans.push_back({st, ed});
133
+ return ans;
148
134
}
149
135
};
150
136
```
@@ -153,63 +139,48 @@ public:
153
139
154
140
```go
155
141
func merge(intervals [][]int) [][]int {
156
- var res [][]int
157
142
sort.Slice(intervals, func(i, j int) bool {
158
143
return intervals[i][0] < intervals[j][0]
159
144
})
160
- st, ed := -1, -1
161
- for _, e := range intervals {
145
+ st, ed := intervals[0][0], intervals[0][1]
146
+ var ans [][]int
147
+ for _, e := range intervals[1:] {
162
148
if ed < e[0] {
163
- if st != -1 {
164
- res = append(res, []int{st, ed})
165
- }
149
+ ans = append(ans, []int{st, ed})
166
150
st, ed = e[0], e[1]
167
- } else {
168
- ed = max(ed, e[1])
151
+ } else if ed < e[1] {
152
+ ed = e[1]
169
153
}
170
154
}
171
- if st != -1 {
172
- res = append(res, []int{st, ed})
173
- }
174
- return res
175
- }
176
-
177
- func max(a, b int) int {
178
- if a > b {
179
- return a
180
- }
181
- return b
155
+ ans = append(ans, []int{st, ed})
156
+ return ans
182
157
}
183
158
```
184
159
185
160
### ** C#**
186
161
187
- ``` cpp
162
+ ``` cs
188
163
public class Solution {
189
164
public int [][] Merge (int [][] intervals ) {
190
- var res = new List<int[ ] >();
191
- int st = -1, ed = -1;
192
- foreach (var e in intervals.OrderBy(a => a[ 0] ))
165
+ intervals = intervals .OrderBy (a => a [0 ]).ToArray ();
166
+ int st = intervals [0 ][0 ], ed = intervals [0 ][1 ];
167
+ var ans = new List <int []>();
168
+ for (int i = 1 ; i < intervals .Length ; ++ i )
193
169
{
194
- if (ed < e[ 0] )
170
+ int s = intervals [i ][0 ], e = intervals [i ][1 ];
171
+ if (ed < s )
195
172
{
196
- if (st != -1)
197
- {
198
- res.Add(new int[ ] { st, ed });
199
- }
200
- st = e[ 0] ;
201
- ed = e[ 1] ;
173
+ ans .Add (new int []{st , ed });
174
+ st = s ;
175
+ ed = e ;
202
176
}
203
177
else
204
178
{
205
- ed = Math.Max(ed, e[ 1 ] );
179
+ ed = Math .Max (ed , e );
206
180
}
207
181
}
208
- if (st != -1)
209
- {
210
- res.Add(new int[ ] { st, ed });
211
- }
212
- return res.ToArray();
182
+ ans .Add (new int []{st , ed });
183
+ return ans .ToArray ();
213
184
}
214
185
}
215
186
```
0 commit comments