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 7bb37c1

Browse files
committed
commit solution 86 209 557
1 parent fc81271 commit 7bb37c1

File tree

14 files changed

+337
-12
lines changed

14 files changed

+337
-12
lines changed

‎index-tags.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
2222
| [189](https://leetcode-cn.com/problems/rotate-array) | [旋转数组](/solution/100-199/0189.rotate-array/) | `数组` | <font color=green>简单</font> ||
2323
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
24+
| [209](https://leetcode-cn.com/problems/minimum-size-subarray-sum) | [长度最小的子数组](/solution/200-299/0209.minimum-size-subarray-sum/) | `数组`,`双指针`,`二分查找` | <font color=blue>中等</font> ||
2425
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
2526
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
2627
| [283](https://leetcode-cn.com/problems/move-zeroes) | [移动零](/solution/200-299/0283.move-zeroes/) | `数组`,`双指针` | <font color=green>简单</font> ||
@@ -39,6 +40,7 @@
3940
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
4041
| [344](https://leetcode-cn.com/problems/reverse-string) | [反转字符串](/solution/300-399/0344.reverse-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
4142
| [345](https://leetcode-cn.com/problems/reverse-vowels-of-a-string) | [反转字符串中的元音字母](/solution/300-399/0345.reverse-vowels-of-a-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
43+
| [557](https://leetcode-cn.com/problems/reverse-words-in-a-string-iii) | [反转字符串中的单词 iii](/solution/500-599/0557.reverse-words-in-a-string-iii/) | `字符串` | <font color=green>简单</font> ||
4244

4345
#### **数学**
4446

@@ -86,9 +88,11 @@
8688
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||
8789
| [75](https://leetcode-cn.com/problems/sort-colors) | [颜色分类](/solution/1-99/0075.sort-colors/) | `排序`,`数组`,`双指针` | <font color=blue>中等</font> ||
8890
| [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> ||
91+
| [86](https://leetcode-cn.com/problems/partition-list) | [分隔链表](/solution/1-99/0086.partition-list/) | `链表`,`双指针` | <font color=blue>中等</font> ||
8992
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
9093
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
9194
| [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> ||
95+
| [209](https://leetcode-cn.com/problems/minimum-size-subarray-sum) | [长度最小的子数组](/solution/200-299/0209.minimum-size-subarray-sum/) | `数组`,`双指针`,`二分查找` | <font color=blue>中等</font> ||
9296
| [283](https://leetcode-cn.com/problems/move-zeroes) | [移动零](/solution/200-299/0283.move-zeroes/) | `数组`,`双指针` | <font color=green>简单</font> ||
9397
| [344](https://leetcode-cn.com/problems/reverse-string) | [反转字符串](/solution/300-399/0344.reverse-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
9498
| [345](https://leetcode-cn.com/problems/reverse-vowels-of-a-string) | [反转字符串中的元音字母](/solution/300-399/0345.reverse-vowels-of-a-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
@@ -100,6 +104,7 @@
100104
| [2](https://leetcode-cn.com/problems/add-two-numbers) | [两数相加](/solution/1-99/0002.add-two-numbers/) | `链表`,`数学` | <font color=blue>中等</font> ||
101105
| [21](https://leetcode-cn.com/problems/merge-two-sorted-lists) | [合并两个有序链表](/solution/1-99/0021.merge-two-sorted-lists/) | `链表` | <font color=green>简单</font> ||
102106
| [83](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list) | [删除排序链表中的重复元素](/solution/1-99/0083.remove-duplicates-from-sorted-list/) | `链表` | <font color=green>简单</font> ||
107+
| [86](https://leetcode-cn.com/problems/partition-list) | [分隔链表](/solution/1-99/0086.partition-list/) | `链表`,`双指针` | <font color=blue>中等</font> ||
103108
| [160](https://leetcode-cn.com/problems/intersection-of-two-linked-lists) | [相交链表](/solution/100-199/0160.intersection-of-two-linked-lists/) | `链表` | <font color=green>简单</font> ||
104109

105110
#### **二分查找**
@@ -109,6 +114,7 @@
109114
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
110115
| [50](https://leetcode-cn.com/problems/powx-n) | [pow(x, n)](/solution/1-99/0050.pow%28x%2c-n%29/) | `数学`,`二分查找` | <font color=blue>中等</font> ||
111116
| [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> ||
117+
| [209](https://leetcode-cn.com/problems/minimum-size-subarray-sum) | [长度最小的子数组](/solution/200-299/0209.minimum-size-subarray-sum/) | `数组`,`双指针`,`二分查找` | <font color=blue>中等</font> ||
112118

113119
#### **排序**
114120

‎index-type.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
| [541](https://leetcode-cn.com/problems/reverse-string-ii) | [反转字符串 ii](/solution/500-599/0541.reverse-string-ii/) | `字符串` | <font color=green>简单</font> |
156156
| [543](https://leetcode-cn.com/problems/diameter-of-binary-tree) | [二叉树的直径](/solution/500-599/0543.diameter-of-binary-tree/) | `` | <font color=green>简单</font> |
157157
| [551](https://leetcode-cn.com/problems/student-attendance-record-i) | [学生出勤记录 i](/solution/500-599/0551.student-attendance-record-i/) | `字符串` | <font color=green>简单</font> |
158-
| [557](https://leetcode-cn.com/problems/reverse-words-in-a-string-iii) | [反转字符串中的单词 iii](/solution/500-599/0557.reverse-words-in-a-string-iii/) | `字符串` | <font color=green>简单</font> |
158+
| [557](https://leetcode-cn.com/problems/reverse-words-in-a-string-iii) | [反转字符串中的单词 iii](/solution/500-599/0557.reverse-words-in-a-string-iii/) | `字符串` | <font color=green>简单</font> ||
159159
| [559](https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree) | [n叉树的最大深度](/solution/500-599/0559.maximum-depth-of-n-ary-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> |
160160
| [561](https://leetcode-cn.com/problems/array-partition-i) | [数组拆分 i](/solution/500-599/0561.array-partition-i/) | `数组` | <font color=green>简单</font> |
161161
| [563](https://leetcode-cn.com/problems/binary-tree-tilt) | [二叉树的坡度](/solution/500-599/0563.binary-tree-tilt/) | `` | <font color=green>简单</font> |
@@ -451,7 +451,7 @@
451451
| [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> ||
452452
| [81](https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii) | [搜索旋转排序数组 ii](/solution/1-99/0081.search-in-rotated-sorted-array-ii/) | `数组`,`二分查找` | <font color=blue>中等</font> |
453453
| [82](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii) | [删除排序链表中的重复元素 ii](/solution/1-99/0082.remove-duplicates-from-sorted-list-ii/) | `链表` | <font color=blue>中等</font> |
454-
| [86](https://leetcode-cn.com/problems/partition-list) | [分隔链表](/solution/1-99/0086.partition-list/) | `链表`,`双指针` | <font color=blue>中等</font> |
454+
| [86](https://leetcode-cn.com/problems/partition-list) | [分隔链表](/solution/1-99/0086.partition-list/) | `链表`,`双指针` | <font color=blue>中等</font> ||
455455
| [89](https://leetcode-cn.com/problems/gray-code) | [格雷编码](/solution/1-99/0089.gray-code/) | `回溯算法` | <font color=blue>中等</font> |
456456
| [90](https://leetcode-cn.com/problems/subsets-ii) | [子集 ii](/solution/1-99/0090.subsets-ii/) | `数组`,`回溯算法` | <font color=blue>中等</font> |
457457
| [91](https://leetcode-cn.com/problems/decode-ways) | [解码方法](/solution/1-99/0091.decode-ways/) | `字符串`,`动态规划` | <font color=blue>中等</font> |
@@ -512,7 +512,7 @@
512512
| [201](https://leetcode-cn.com/problems/bitwise-and-of-numbers-range) | [数字范围按位与](/solution/200-299/0201.bitwise-and-of-numbers-range/) | `位运算` | <font color=blue>中等</font> |
513513
| [207](https://leetcode-cn.com/problems/course-schedule) | [课程表](/solution/200-299/0207.course-schedule/) | `深度优先搜索`,`广度优先搜索`,``,`拓扑排序` | <font color=blue>中等</font> |
514514
| [208](https://leetcode-cn.com/problems/implement-trie-prefix-tree) | [实现 trie (前缀树)](/solution/200-299/0208.implement-trie-%28prefix-tree%29/) | `设计`,`字典树` | <font color=blue>中等</font> |
515-
| [209](https://leetcode-cn.com/problems/minimum-size-subarray-sum) | [长度最小的子数组](/solution/200-299/0209.minimum-size-subarray-sum/) | `数组`,`双指针`,`二分查找` | <font color=blue>中等</font> |
515+
| [209](https://leetcode-cn.com/problems/minimum-size-subarray-sum) | [长度最小的子数组](/solution/200-299/0209.minimum-size-subarray-sum/) | `数组`,`双指针`,`二分查找` | <font color=blue>中等</font> ||
516516
| [210](https://leetcode-cn.com/problems/course-schedule-ii) | [课程表 ii](/solution/200-299/0210.course-schedule-ii/) | `深度优先搜索`,`广度优先搜索`,``,`拓扑排序` | <font color=blue>中等</font> |
517517
| [211](https://leetcode-cn.com/problems/add-and-search-word-data-structure-design) | [添加与搜索单词 - 数据结构设计](/solution/200-299/0211.add-and-search-word---data-structure-design/) | `设计`,`字典树`,`回溯算法` | <font color=blue>中等</font> |
518518
| [213](https://leetcode-cn.com/problems/house-robber-ii) | [打家劫舍 ii](/solution/200-299/0213.house-robber-ii/) | `动态规划` | <font color=blue>中等</font> |

‎solution/1-99/0086.partition-list/README.md

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [86. 分隔链表](https://leetcode-cn.com/problems/partition-list)
22

33
### 题目描述
4-
<!-- 这里写题目描述 -->
4+
55
<p>给定一个链表和一个特定值<em> x</em>,对链表进行分隔,使得所有小于 <em>x</em> 的节点都在大于或等于 <em>x</em> 的节点之前。</p>
66

77
<p>你应当保留两个分区中每个节点的初始相对位置。</p>
@@ -16,15 +16,34 @@
1616

1717
### 解题思路
1818

19+
1. 双指针
1920

2021
### 具体解法
2122

22-
<!-- tabs:start -->
23-
2423
#### **Golang**
2524
```go
26-
25+
type ListNode struct {
26+
Val int
27+
Next *ListNode
28+
}
29+
30+
func partition(head *ListNode, x int) *ListNode {
31+
lessTop := &ListNode{}
32+
less := lessTop
33+
greaterTop := &ListNode{}
34+
greater := greaterTop
35+
for n := head; n != nil; n = n.Next {
36+
if n.Val < x {
37+
less.Next = n
38+
less = n
39+
continue
40+
}
41+
greater.Next = n
42+
greater = n
43+
}
44+
less.Next = greaterTop.Next
45+
greater.Next = nil
46+
return lessTop.Next
47+
}
2748
```
2849

29-
<!-- tabs:end -->
30-
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=86 lang=golang
5+
*
6+
* [86] 分隔链表
7+
*/
8+
9+
// @lc code=start
10+
/**
11+
* Definition for singly-linked list.
12+
* type ListNode struct {
13+
* Val int
14+
* Next *ListNode
15+
* }
16+
*/
17+
18+
type ListNode struct {
19+
Val int
20+
Next *ListNode
21+
}
22+
23+
func partition(head *ListNode, x int) *ListNode {
24+
lessTop := &ListNode{}
25+
less := lessTop
26+
greaterTop := &ListNode{}
27+
greater := greaterTop
28+
for n := head; n != nil; n = n.Next {
29+
if n.Val < x {
30+
less.Next = n
31+
less = n
32+
continue
33+
}
34+
greater.Next = n
35+
greater = n
36+
}
37+
less.Next = greaterTop.Next
38+
greater.Next = nil
39+
return lessTop.Next
40+
}
41+
42+
// @lc code=end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestPartition(t *testing.T) {
8+
var x int
9+
x = 3
10+
listNode := &ListNode{
11+
Val: 1,
12+
Next: &ListNode{
13+
Val: 4,
14+
Next: &ListNode{
15+
Val: 3,
16+
Next: &ListNode{
17+
Val: 2,
18+
Next: &ListNode{
19+
Val: 5,
20+
Next: &ListNode{
21+
Val: 2,
22+
Next: nil,
23+
},
24+
},
25+
},
26+
},
27+
},
28+
}
29+
partition(listNode, x)
30+
}

‎solution/1-99/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
- [83. 删除排序链表中的重复元素 ✅](solution/1-99/0083.remove-duplicates-from-sorted-list/)
9292
- [84. 柱状图中最大的矩形](solution/1-99/0084.largest-rectangle-in-histogram/)
9393
- [85. 最大矩形](solution/1-99/0085.maximal-rectangle/)
94-
- [86. 分隔链表](solution/1-99/0086.partition-list/)
94+
- [86. 分隔链表](solution/1-99/0086.partition-list/)
9595
- [87. 扰乱字符串](solution/1-99/0087.scramble-string/)
9696
- [88. 合并两个有序数组 ✅](solution/1-99/0088.merge-sorted-array/)
9797
- [89. 格雷编码](solution/1-99/0089.gray-code/)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [209. 长度最小的子数组](https://leetcode-cn.com/problems/minimum-size-subarray-sum/description/)
2+
3+
### 题目描述
4+
5+
<p>给定一个含有&nbsp;<strong>n&nbsp;</strong>个正整数的数组和一个正整数&nbsp;<strong>s ,</strong>找出该数组中满足其和<strong> &ge; s </strong>的长度最小的连续子数组,并返回其长度<strong>。</strong>如果不存在符合条件的连续子数组,返回 0。</p>
6+
7+
<p>&nbsp;</p>
8+
9+
<p><strong>示例:</strong></p>
10+
11+
<pre><strong>输入:</strong><code>s = 7, nums = [2,3,1,2,4,3]</code>
12+
<strong>输出:</strong>2
13+
<strong>解释:</strong>子数组&nbsp;<code>[4,3]</code>&nbsp;是该条件下的长度最小的连续子数组。
14+
</pre>
15+
16+
<p>&nbsp;</p>
17+
18+
<p><strong>进阶:</strong></p>
19+
20+
<ul>
21+
<li>如果你已经完成了<em> O</em>(<em>n</em>) 时间复杂度的解法, 请尝试 <em>O</em>(<em>n</em> log <em>n</em>) 时间复杂度的解法。</li>
22+
</ul>
23+
24+
### 解题思路
25+
26+
定义两个指针 start 和 end 分别表示子数组的开始位置和结束位置,维护变量 sum 存储子数组中的元素和(即从 []nums[start][]nums[end] 的元素和)。
27+
28+
初始状态下,start 和 end 都指向下标 00,sum 的值为 00。
29+
30+
每一轮迭代,将 [end]nums[end] 加到 sum,如果 \ge ssum≥s,则更新子数组的最小长度(此时子数组的长度是 -+1end−start+1),然后将 [start]nums[start] 从 sum 中减去并将 start 右移,直到 sum < s,在此过程中同样更新子数组的最小长度。在每一轮迭代的最后,将 end 右移。
31+
32+
33+
### 具体解法
34+
35+
36+
#### **Golang**
37+
```go
38+
func minSubArrayLen(s int, nums []int) int {
39+
40+
if len(nums) == 0 {
41+
return 0
42+
}
43+
ans := math.MaxInt32
44+
sum := 0
45+
start := 0
46+
47+
for end := 0; end < len(nums); end++ {
48+
sum += nums[end]
49+
for sum >= s {
50+
ans = min(end-start+1, ans)
51+
sum -= nums[start]
52+
start++
53+
}
54+
}
55+
if ans == math.MaxInt32 {
56+
return 0
57+
}
58+
return ans
59+
}
60+
61+
func min(i, j int) int {
62+
if i < j {
63+
return i
64+
}
65+
return j
66+
}
67+
```
68+
69+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
import "math"
4+
5+
/*
6+
* @lc app=leetcode.cn id=209 lang=golang
7+
*
8+
* [209] 长度最小的子数组
9+
*/
10+
11+
// @lc code=start
12+
func minSubArrayLen(s int, nums []int) int {
13+
14+
if len(nums) == 0 {
15+
return 0
16+
}
17+
ans := math.MaxInt32
18+
sum := 0
19+
start := 0
20+
21+
for end := 0; end < len(nums); end++ {
22+
sum += nums[end]
23+
for sum >= s {
24+
ans = min(end-start+1, ans)
25+
sum -= nums[start]
26+
start++
27+
}
28+
}
29+
if ans == math.MaxInt32 {
30+
return 0
31+
}
32+
return ans
33+
}
34+
35+
func min(i, j int) int {
36+
if i < j {
37+
return i
38+
}
39+
return j
40+
}
41+
42+
// @lc code=end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMinSubArrayLen(t *testing.T) {
8+
var s int
9+
var nums []int
10+
var ret int
11+
nums = []int{2, 3, 1, 2, 4, 3}
12+
ret = 2
13+
s = 7
14+
if ret != minSubArrayLen(s, nums) {
15+
t.Fatalf("case fails %v\n", ret)
16+
}
17+
18+
nums = []int{}
19+
ret = 0
20+
s = 7
21+
if ret != minSubArrayLen(s, nums) {
22+
t.Fatalf("case fails %v\n", ret)
23+
}
24+
}

‎solution/200-299/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
- [206. 反转链表](solution/200-299/0206.reverse-linked-list/)
1616
- [207. 课程表](solution/200-299/0207.course-schedule/)
1717
- [208. 实现 trie (前缀树)](solution/200-299/0208.implement-trie-prefix-tree/)
18-
- [209. 长度最小的子数组](solution/200-299/0209.minimum-size-subarray-sum/)
18+
- [209. 长度最小的子数组](solution/200-299/0209.minimum-size-subarray-sum/)
1919
- [210. 课程表 ii](solution/200-299/0210.course-schedule-ii/)
2020
- [211. 添加与搜索单词 - 数据结构设计](solution/200-299/0211.add-and-search-word---data-structure-design/)
2121
- [212. 单词搜索 ii](solution/200-299/0212.word-search-ii/)

0 commit comments

Comments
(0)

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