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