Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f15d84f

Browse files
committed
commit solution 56
1 parent bc6d2da commit f15d84f

File tree

6 files changed

+132
-7
lines changed

6 files changed

+132
-7
lines changed

‎index-tags.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
| [34](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array) | [在排序数组中查找元素的第一个和最后一个位置](/solution/1-99/0034.find-first-and-last-position-of-element-in-sorted-array/) | `数组`,`二分查找` | <font color=blue>中等</font> ||
1515
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
1616
| [53](https://leetcode-cn.com/problems/maximum-subarray) | [最大子序和](/solution/1-99/0053.maximum-subarray/) | `数组`,`分治算法`,`动态规划` | <font color=green>简单</font> ||
17+
| [56](https://leetcode-cn.com/problems/merge-intervals) | [合并区间](/solution/1-99/0056.merge-intervals/) | `排序`,`数组` | <font color=blue>中等</font> ||
1718
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> ||
1819
| [75](https://leetcode-cn.com/problems/sort-colors) | [颜色分类](/solution/1-99/0075.sort-colors/) | `排序`,`数组`,`双指针` | <font color=blue>中等</font> ||
1920
| [80](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii) | [删除排序数组中的重复项 ii](/solution/1-99/0080.remove-duplicates-from-sorted-array-ii/) | `数组`,`双指针` | <font color=blue>中等</font> ||
2021
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
2122
| [118](https://leetcode-cn.com/problems/pascals-triangle) | [杨辉三角](/solution/100-199/0118.pascal%27s-triangle/) | `数组` | <font color=green>简单</font> ||
23+
| [121](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock) | [买卖股票的最佳时机](/solution/100-199/0121.best-time-to-buy-and-sell-stock/) | `数组`,`动态规划` | <font color=green>简单</font> ||
2224
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> ||
2325
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
2426
| [189](https://leetcode-cn.com/problems/rotate-array) | [旋转数组](/solution/100-199/0189.rotate-array/) | `数组` | <font color=green>简单</font> ||
@@ -133,7 +135,9 @@
133135

134136
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
135137
| --- | --- | --- | --- | --- |
138+
| [56](https://leetcode-cn.com/problems/merge-intervals) | [合并区间](/solution/1-99/0056.merge-intervals/) | `排序`,`数组` | <font color=blue>中等</font> ||
136139
| [75](https://leetcode-cn.com/problems/sort-colors) | [颜色分类](/solution/1-99/0075.sort-colors/) | `排序`,`数组`,`双指针` | <font color=blue>中等</font> ||
140+
137141
#### **队列**
138142

139143
#### **位运算**
@@ -192,6 +196,7 @@
192196
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
193197
| --- | --- | --- | --- | --- |
194198
| [53](https://leetcode-cn.com/problems/maximum-subarray) | [最大子序和](/solution/1-99/0053.maximum-subarray/) | `数组`,`分治算法`,`动态规划` | <font color=green>简单</font> ||
199+
| [121](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock) | [买卖股票的最佳时机](/solution/100-199/0121.best-time-to-buy-and-sell-stock/) | `数组`,`动态规划` | <font color=green>简单</font> ||
195200

196201
#### **分治算法**
197202

‎index-type.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
| [112](https://leetcode-cn.com/problems/path-sum) | [路径总和](/solution/100-199/0112.path-sum/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
3838
| [118](https://leetcode-cn.com/problems/pascals-triangle) | [杨辉三角](/solution/100-199/0118.pascal%27s-triangle/) | `数组` | <font color=green>简单</font> ||
3939
| [119](https://leetcode-cn.com/problems/pascals-triangle-ii) | [杨辉三角 ii](/solution/100-199/0119.pascal%27s-triangle-ii/) | `数组` | <font color=green>简单</font> |
40-
| [121](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock) | [买卖股票的最佳时机](/solution/100-199/0121.best-time-to-buy-and-sell-stock/) | `数组`,`动态规划` | <font color=green>简单</font> |
40+
| [121](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock) | [买卖股票的最佳时机](/solution/100-199/0121.best-time-to-buy-and-sell-stock/) | `数组`,`动态规划` | <font color=green>简单</font> ||
4141
| [122](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii) | [买卖股票的最佳时机 ii](/solution/100-199/0122.best-time-to-buy-and-sell-stock-ii/) | `贪心算法`,`数组` | <font color=green>简单</font> |
4242
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
4343
| [136](https://leetcode-cn.com/problems/single-number) | [只出现一次的数字](/solution/100-199/0136.single-number/) | `位运算`,`哈希表` | <font color=green>简单</font> ||
@@ -434,7 +434,7 @@
434434
| [50](https://leetcode-cn.com/problems/powx-n) | [pow(x, n)](/solution/1-99/0050.pow%28x%2c-n%29/) | `数学`,`二分查找` | <font color=blue>中等</font> ||
435435
| [54](https://leetcode-cn.com/problems/spiral-matrix) | [螺旋矩阵](/solution/1-99/0054.spiral-matrix/) | `数组` | <font color=blue>中等</font> |
436436
| [55](https://leetcode-cn.com/problems/jump-game) | [跳跃游戏](/solution/1-99/0055.jump-game/) | `贪心算法`,`数组` | <font color=blue>中等</font> |
437-
| [56](https://leetcode-cn.com/problems/merge-intervals) | [合并区间](/solution/1-99/0056.merge-intervals/) | `排序`,`数组` | <font color=blue>中等</font> |
437+
| [56](https://leetcode-cn.com/problems/merge-intervals) | [合并区间](/solution/1-99/0056.merge-intervals/) | `排序`,`数组` | <font color=blue>中等</font> ||
438438
| [59](https://leetcode-cn.com/problems/spiral-matrix-ii) | [螺旋矩阵 ii](/solution/1-99/0059.spiral-matrix-ii/) | `数组` | <font color=blue>中等</font> |
439439
| [60](https://leetcode-cn.com/problems/permutation-sequence) | [第k个排列](/solution/1-99/0060.permutation-sequence/) | `数学`,`回溯算法` | <font color=blue>中等</font> |
440440
| [61](https://leetcode-cn.com/problems/rotate-list) | [旋转链表](/solution/1-99/0061.rotate-list/) | `链表`,`双指针` | <font color=blue>中等</font> |

‎solution/1-99/0056.merge-intervals/README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [56. 合并区间](https://leetcode-cn.com/problems/merge-intervals)
22

33
### 题目描述
4-
<!-- 这里写题目描述 -->
4+
55
<p>给出一个区间的集合,请合并所有重叠的区间。</p>
66

77
<p><strong>示例 1:</strong></p>
@@ -24,12 +24,42 @@
2424

2525
### 具体解法
2626

27-
<!-- tabs:start -->
2827

2928
#### **Golang**
3029
```go
31-
30+
func merge(intervals [][]int) [][]int {
31+
var res [][]int
32+
if len(intervals) == 0 {
33+
return res
34+
}
35+
if len(intervals) == 1 {
36+
return intervals
37+
}
38+
sort.Slice(intervals, func(i, j int) bool {
39+
return intervals[i][0] < intervals[j][0]
40+
})
41+
tmp := intervals[0]
42+
flag := false
43+
for _, nums := range intervals[1:] {
44+
if tmp[1] >= nums[0] && tmp[0] <= nums[1] {
45+
if tmp[1] < nums[1] {
46+
tmp[1] = nums[1]
47+
}
48+
if tmp[0] > nums[0] {
49+
tmp[0] = nums[0]
50+
}
51+
flag = true
52+
} else {
53+
res = append(res, tmp)
54+
flag = false
55+
tmp = nums
56+
}
57+
}
58+
if flag == true || tmp[0] == intervals[len(intervals)-1][0] {
59+
res = append(res, tmp)
60+
}
61+
return res
62+
}
3263
```
3364

34-
<!-- tabs:end -->
3565

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package leetcode
2+
3+
import (
4+
"sort"
5+
)
6+
7+
/*
8+
* @lc app=leetcode.cn id=56 lang=golang
9+
*
10+
* [56] 合并区间
11+
*/
12+
13+
// @lc code=start
14+
func merge(intervals [][]int) [][]int {
15+
var res [][]int
16+
if len(intervals) == 0 {
17+
return res
18+
}
19+
if len(intervals) == 1 {
20+
return intervals
21+
}
22+
sort.Slice(intervals, func(i, j int) bool {
23+
return intervals[i][0] < intervals[j][0]
24+
})
25+
tmp := intervals[0]
26+
flag := false
27+
for _, nums := range intervals[1:] {
28+
if tmp[1] >= nums[0] && tmp[0] <= nums[1] {
29+
if tmp[1] < nums[1] {
30+
tmp[1] = nums[1]
31+
}
32+
if tmp[0] > nums[0] {
33+
tmp[0] = nums[0]
34+
}
35+
flag = true
36+
} else {
37+
res = append(res, tmp)
38+
flag = false
39+
tmp = nums
40+
}
41+
}
42+
if flag == true || tmp[0] == intervals[len(intervals)-1][0] {
43+
res = append(res, tmp)
44+
}
45+
return res
46+
}
47+
48+
// @lc code=end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestMerge(t *testing.T) {
9+
var nums [][]int
10+
11+
nums = [][]int{{1, 4}, {4, 5}}
12+
13+
fmt.Println(merge(nums))
14+
15+
nums = [][]int{{1, 3}}
16+
17+
fmt.Println(merge(nums))
18+
19+
nums = [][]int{{1, 4}, {5, 6}}
20+
21+
fmt.Println(merge(nums))
22+
23+
nums = [][]int{{1, 6}, {2, 6}, {8, 10}, {15, 18}}
24+
25+
fmt.Println(merge(nums))
26+
27+
nums = [][]int{{1, 4}, {0, 4}}
28+
29+
fmt.Println(merge(nums))
30+
31+
nums = [][]int{{1, 4}, {0, 1}}
32+
33+
fmt.Println(merge(nums))
34+
35+
nums = [][]int{{1, 4}, {0, 0}}
36+
37+
fmt.Println(merge(nums))
38+
39+
nums = [][]int{{2, 3}, {4, 5}, {6, 7}, {8, 9}, {1, 10}}
40+
41+
fmt.Println(merge(nums))
42+
}

‎solution/1-99/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
- [53. 最大子序和 ✅](solution/1-99/0053.maximum-subarray/)
6262
- [54. 螺旋矩阵](solution/1-99/0054.spiral-matrix/)
6363
- [55. 跳跃游戏](solution/1-99/0055.jump-game/)
64-
- [56. 合并区间](solution/1-99/0056.merge-intervals/)
64+
- [56. 合并区间](solution/1-99/0056.merge-intervals/)
6565
- [57. 插入区间](solution/1-99/0057.insert-interval/)
6666
- [58. 最后一个单词的长度 ✅](solution/1-99/0058.length-of-last-word/)
6767
- [59. 螺旋矩阵 ii](solution/1-99/0059.spiral-matrix-ii/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /