Skip to content

Commit 851946d

Browse files
committed
Update solution 0003
1 parent e72e4cb commit 851946d

File tree

7 files changed

+49
-37
lines changed

7 files changed

+49
-37
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@
364364
|0223|Rectangle Area|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0223.Rectangle-Area)|38.5%|Medium||
365365
|0224|Basic Calculator|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0224.Basic-Calculator)|38.4%|Hard||
366366
|0225|Implement Stack using Queues|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0225.Implement-Stack-using-Queues)|48.2%|Easy||
367-
|0226|Invert Binary Tree|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0226.Invert-Binary-Tree)|67.7%|Easy||
367+
|0226|Invert Binary Tree|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0226.Invert-Binary-Tree)|67.6%|Easy||
368368
|0227|Basic Calculator II|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0227.Basic-Calculator-II)|38.9%|Medium||
369369
|0228|Summary Ranges|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0228.Summary-Ranges)|42.9%|Easy||
370370
|0229|Majority Element II|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0229.Majority-Element-II)|39.3%|Medium||
@@ -954,7 +954,7 @@
954954
|0813|Largest Sum of Averages||51.3%|Medium||
955955
|0814|Binary Tree Pruning||71.8%|Medium||
956956
|0815|Bus Routes|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0815.Bus-Routes)|43.6%|Hard||
957-
|0816|Ambiguous Coordinates|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0816.Ambiguous-Coordinates)|48.8%|Medium||
957+
|0816|Ambiguous Coordinates|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0816.Ambiguous-Coordinates)|49.0%|Medium||
958958
|0817|Linked List Components|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0817.Linked-List-Components)|57.8%|Medium||
959959
|0818|Race Car||40.6%|Hard||
960960
|0819|Most Common Word|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0819.Most-Common-Word)|45.5%|Easy||
@@ -1231,7 +1231,7 @@
12311231
|1090|Largest Values From Labels||60.2%|Medium||
12321232
|1091|Shortest Path in Binary Matrix|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1091.Shortest-Path-in-Binary-Matrix)|40.3%|Medium||
12331233
|1092|Shortest Common Supersequence||53.4%|Hard||
1234-
|1093|Statistics from a Large Sample|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1093.Statistics-from-a-Large-Sample)|48.3%|Medium||
1234+
|1093|Statistics from a Large Sample|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1093.Statistics-from-a-Large-Sample)|48.4%|Medium||
12351235
|1094|Car Pooling||59.7%|Medium||
12361236
|1095|Find in Mountain Array||36.1%|Hard||
12371237
|1096|Brace Expansion II||62.9%|Hard||
@@ -1291,7 +1291,7 @@
12911291
|1150|Check If a Number Is Majority Element in a Sorted Array||57.0%|Easy||
12921292
|1151|Minimum Swaps to Group All 1's Together||58.8%|Medium||
12931293
|1152|Analyze User Website Visit Pattern||43.0%|Medium||
1294-
|1153|String Transforms Into Another String||35.8%|Hard||
1294+
|1153|String Transforms Into Another String||35.7%|Hard||
12951295
|1154|Day of the Year|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1154.Day-of-the-Year)|49.2%|Easy||
12961296
|1155|Number of Dice Rolls With Target Sum||47.6%|Medium||
12971297
|1156|Swap For Longest Repeated Character Substring||47.0%|Medium||
@@ -1310,7 +1310,7 @@
13101310
|1169|Invalid Transactions||30.7%|Medium||
13111311
|1170|Compare Strings by Frequency of the Smallest Character|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1170.Compare-Strings-by-Frequency-of-the-Smallest-Character)|60.5%|Medium||
13121312
|1171|Remove Zero Sum Consecutive Nodes from Linked List|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1171.Remove-Zero-Sum-Consecutive-Nodes-from-Linked-List)|41.5%|Medium||
1313-
|1172|Dinner Plate Stacks||37.4%|Hard||
1313+
|1172|Dinner Plate Stacks||37.5%|Hard||
13141314
|1173|Immediate Food Delivery I||82.8%|Easy||
13151315
|1174|Immediate Food Delivery II||62.4%|Medium||
13161316
|1175|Prime Arrangements|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1175.Prime-Arrangements)|51.7%|Easy||
@@ -1436,7 +1436,7 @@
14361436
|1295|Find Numbers with Even Number of Digits|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1295.Find-Numbers-with-Even-Number-of-Digits)|78.6%|Easy||
14371437
|1296|Divide Array in Sets of K Consecutive Numbers||55.8%|Medium||
14381438
|1297|Maximum Number of Occurrences of a Substring||51.1%|Medium||
1439-
|1298|Maximum Candies You Can Get from Boxes||60.1%|Hard||
1439+
|1298|Maximum Candies You Can Get from Boxes||60.0%|Hard||
14401440
|1299|Replace Elements with Greatest Element on Right Side|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1299.Replace-Elements-with-Greatest-Element-on-Right-Side)|74.5%|Easy||
14411441
|1300|Sum of Mutated Array Closest to Target|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1300.Sum-of-Mutated-Array-Closest-to-Target)|43.0%|Medium||
14421442
|1301|Number of Paths with Max Score||38.3%|Hard||
@@ -1531,7 +1531,7 @@
15311531
|1390|Four Divisors||39.7%|Medium||
15321532
|1391|Check if There is a Valid Path in a Grid||45.2%|Medium||
15331533
|1392|Longest Happy Prefix||42.3%|Hard||
1534-
|1393|Capital Gain/Loss||91.2%|Medium||
1534+
|1393|Capital Gain/Loss||91.1%|Medium||
15351535
|1394|Find Lucky Integer in an Array||63.0%|Easy||
15361536
|1395|Count Number of Teams||74.1%|Medium||
15371537
|1396|Design Underground System|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1396.Design-Underground-System)|71.8%|Medium||
@@ -1544,7 +1544,7 @@
15441544
|1403|Minimum Subsequence in Non-Increasing Order||71.9%|Easy||
15451545
|1404|Number of Steps to Reduce a Number in Binary Representation to One||50.1%|Medium||
15461546
|1405|Longest Happy String||53.0%|Medium||
1547-
|1406|Stone Game III||58.3%|Hard||
1547+
|1406|Stone Game III||58.4%|Hard||
15481548
|1407|Top Travellers||83.9%|Easy||
15491549
|1408|String Matching in an Array||63.7%|Easy||
15501550
|1409|Queries on a Permutation With Key||81.9%|Medium||
@@ -1883,7 +1883,7 @@
18831883
|1742|Maximum Number of Balls in a Box|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1742.Maximum-Number-of-Balls-in-a-Box)|73.2%|Easy||
18841884
|1743|Restore the Array From Adjacent Pairs||63.9%|Medium||
18851885
|1744|Can You Eat Your Favorite Candy on Your Favorite Day?||30.8%|Medium||
1886-
|1745|Palindrome Partitioning IV||49.6%|Hard||
1886+
|1745|Palindrome Partitioning IV||49.5%|Hard||
18871887
|1746|Maximum Subarray Sum After One Operation||61.8%|Medium||
18881888
|1747|Leetflex Banned Accounts||69.2%|Medium||
18891889
|1748|Sum of Unique Elements|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1748.Sum-of-Unique-Elements)|74.7%|Easy||

leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ func lengthOfLongestSubstring(s string) int {
88
var bitSet [256]bool
99
result, left, right := 0, 0, 0
1010
for left < len(s) {
11-
// 右侧字符对应的bitSet被标记true,说明此字符在X位置重复,需要左侧向前移动,直到将X标记为false
11+
// 右侧字符对应的 bitSet 被标记 true,说明此字符在 X 位置重复,需要左侧向前移动,直到将 X 标记为 false
1212
if bitSet[s[right]] {
1313
bitSet[s[left]] = false
1414
left++
@@ -26,23 +26,28 @@ func lengthOfLongestSubstring(s string) int {
2626
return result
2727
}
2828

29-
// 解法二 滑动窗口-数组桶
29+
// 解法二 滑动窗口
3030
func lengthOfLongestSubstring1(s string) int {
31-
right, left, res := 0, 0, 0
32-
var indexes [256]int
31+
if len(s) == 0 {
32+
return 0
33+
}
34+
var freq [256]int
35+
result, left, right := 0, 0, -1
36+
3337
for left < len(s) {
34-
tmp := indexes[s[left]-'a']
35-
if tmp >= right {
36-
right = tmp + 1
38+
if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
39+
freq[s[right+1]-'a']++
40+
right++
41+
} else {
42+
freq[s[left]-'a']--
43+
left++
3744
}
38-
indexes[s[left]-'a'] = left
39-
left++
40-
res = max(res, left-right)
45+
result = max(result, right-left+1)
4146
}
42-
return res
47+
return result
4348
}
4449

45-
// 解法二 滑动窗口-哈希桶
50+
// 解法三 滑动窗口-哈希桶
4651
func lengthOfLongestSubstring2(s string) int {
4752
right, left, res := 0, 0, 0
4853
indexes := make(map[byte]int, len(s))

leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func Test_Problem3(t *testing.T) {
5151

5252
for _, q := range qs {
5353
_, p := q.ans3, q.para3
54-
fmt.Printf("【input】:%v 【output】:%v\n", p, lengthOfLongestSubstring_(p.s))
54+
fmt.Printf("【input】:%v 【output】:%v\n", p, lengthOfLongestSubstring(p.s))
5555
}
5656
fmt.Printf("\n\n\n")
5757
}

website/content/ChapterFour/0001~0099/0003.Longest-Substring-Without-Repeating-Characters.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Explanation: The answer is "wke", with the length of 3.
5252
## 代码
5353

5454
```go
55+
5556
package leetcode
5657

5758
// 解法一 位图
@@ -62,7 +63,7 @@ func lengthOfLongestSubstring(s string) int {
6263
var bitSet [256]bool
6364
result, left, right := 0, 0, 0
6465
for left < len(s) {
65-
// 右侧字符对应的bitSet被标记true,说明此字符在X位置重复,需要左侧向前移动,直到将X标记为false
66+
// 右侧字符对应的 bitSet 被标记 true,说明此字符在 X 位置重复,需要左侧向前移动,直到将 X 标记为 false
6667
if bitSet[s[right]] {
6768
bitSet[s[left]] = false
6869
left++
@@ -80,23 +81,28 @@ func lengthOfLongestSubstring(s string) int {
8081
return result
8182
}
8283

83-
// 解法二 滑动窗口-数组桶
84+
// 解法二 滑动窗口
8485
func lengthOfLongestSubstring1(s string) int {
85-
right, left, res := 0, 0, 0
86-
var indexes [256]int
86+
if len(s) == 0 {
87+
return 0
88+
}
89+
var freq [256]int
90+
result, left, right := 0, 0, -1
91+
8792
for left < len(s) {
88-
tmp := indexes[s[left]-'a']
89-
if tmp >= right {
90-
right = tmp + 1
93+
if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
94+
freq[s[right+1]-'a']++
95+
right++
96+
} else {
97+
freq[s[left]-'a']--
98+
left++
9199
}
92-
indexes[s[left]-'a'] = left
93-
left++
94-
res = max(res, left-right)
100+
result = max(result, right-left+1)
95101
}
96-
return res
102+
return result
97103
}
98104

99-
// 解法二 滑动窗口-哈希桶
105+
// 解法三 滑动窗口-哈希桶
100106
func lengthOfLongestSubstring2(s string) int {
101107
right, left, res := 0, 0, 0
102108
indexes := make(map[byte]int, len(s))
@@ -117,6 +123,7 @@ func max(a int, b int) int {
117123
}
118124
return b
119125
}
126+
120127
```
121128

122129

website/content/ChapterTwo/Math.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ weight: 12
6161
|0836|Rectangle Overlap|[Go]({{< relref "/ChapterFour/0800~0899/0836.Rectangle-Overlap.md" >}})|Easy||||43.3%|
6262
|0869|Reordered Power of 2|[Go]({{< relref "/ChapterFour/0800~0899/0869.Reordered-Power-of-2.md" >}})|Medium||||61.3%|
6363
|0878|Nth Magical Number|[Go]({{< relref "/ChapterFour/0800~0899/0878.Nth-Magical-Number.md" >}})|Hard||||28.9%|
64-
|0885|Spiral Matrix III|[Go]({{< relref "/ChapterFour/0800~0899/0885.Spiral-Matrix-III.md" >}})|Medium| O(n^2)| O(1)||71.1%|
64+
|0885|Spiral Matrix III|[Go]({{< relref "/ChapterFour/0800~0899/0885.Spiral-Matrix-III.md" >}})|Medium| O(n^2)| O(1)||71.2%|
6565
|0887|Super Egg Drop|[Go]({{< relref "/ChapterFour/0800~0899/0887.Super-Egg-Drop.md" >}})|Hard||||27.0%|
6666
|0891|Sum of Subsequence Widths|[Go]({{< relref "/ChapterFour/0800~0899/0891.Sum-of-Subsequence-Widths.md" >}})|Hard| O(n log n)| O(1)||33.2%|
6767
|0892|Surface Area of 3D Shapes|[Go]({{< relref "/ChapterFour/0800~0899/0892.Surface-Area-of-3D-Shapes.md" >}})|Easy||||60.0%|

website/content/ChapterTwo/Sort.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ weight: 14
4646
|1054|Distant Barcodes|[Go]({{< relref "/ChapterFour/1000~1099/1054.Distant-Barcodes.md" >}})|Medium| O(n log n)| O(log n) |❤️|44.4%|
4747
|1122|Relative Sort Array|[Go]({{< relref "/ChapterFour/1100~1199/1122.Relative-Sort-Array.md" >}})|Easy||||68.1%|
4848
|1235|Maximum Profit in Job Scheduling|[Go]({{< relref "/ChapterFour/1200~1299/1235.Maximum-Profit-in-Job-Scheduling.md" >}})|Hard||||47.9%|
49-
|1305|All Elements in Two Binary Search Trees|[Go]({{< relref "/ChapterFour/1300~1399/1305.All-Elements-in-Two-Binary-Search-Trees.md" >}})|Medium||||77.8%|
49+
|1305|All Elements in Two Binary Search Trees|[Go]({{< relref "/ChapterFour/1300~1399/1305.All-Elements-in-Two-Binary-Search-Trees.md" >}})|Medium||||77.9%|
5050
|1329|Sort the Matrix Diagonally|[Go]({{< relref "/ChapterFour/1300~1399/1329.Sort-the-Matrix-Diagonally.md" >}})|Medium||||81.6%|
5151
|1353|Maximum Number of Events That Can Be Attended|[Go]({{< relref "/ChapterFour/1300~1399/1353.Maximum-Number-of-Events-That-Can-Be-Attended.md" >}})|Medium||||30.6%|
5252
|1636|Sort Array by Increasing Frequency|[Go]({{< relref "/ChapterFour/1600~1699/1636.Sort-Array-by-Increasing-Frequency.md" >}})|Easy||||67.0%|

website/content/ChapterTwo/Tree.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ weight: 6
7777
|1123|Lowest Common Ancestor of Deepest Leaves|[Go]({{< relref "/ChapterFour/1100~1199/1123.Lowest-Common-Ancestor-of-Deepest-Leaves.md" >}})|Medium||||68.2%|
7878
|1145|Binary Tree Coloring Game|[Go]({{< relref "/ChapterFour/1100~1199/1145.Binary-Tree-Coloring-Game.md" >}})|Medium||||51.3%|
7979
|1302|Deepest Leaves Sum|[Go]({{< relref "/ChapterFour/1300~1399/1302.Deepest-Leaves-Sum.md" >}})|Medium||||85.4%|
80-
|1305|All Elements in Two Binary Search Trees|[Go]({{< relref "/ChapterFour/1300~1399/1305.All-Elements-in-Two-Binary-Search-Trees.md" >}})|Medium||||77.8%|
80+
|1305|All Elements in Two Binary Search Trees|[Go]({{< relref "/ChapterFour/1300~1399/1305.All-Elements-in-Two-Binary-Search-Trees.md" >}})|Medium||||77.9%|
8181
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|-------------|-------------|
8282

8383

0 commit comments

Comments
 (0)