Update solution 0003

This commit is contained in:
YDZ
2021-05-13 16:10:41 +08:00
parent e72e4cb599
commit 851946dc9a
7 changed files with 53 additions and 41 deletions

View File

@ -364,7 +364,7 @@
|0223|Rectangle Area|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0223.Rectangle-Area)|38.5%|Medium||
|0224|Basic Calculator|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0224.Basic-Calculator)|38.4%|Hard||
|0225|Implement Stack using Queues|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0225.Implement-Stack-using-Queues)|48.2%|Easy||
|0226|Invert Binary Tree|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0226.Invert-Binary-Tree)|67.7%|Easy||
|0226|Invert Binary Tree|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0226.Invert-Binary-Tree)|67.6%|Easy||
|0227|Basic Calculator II|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0227.Basic-Calculator-II)|38.9%|Medium||
|0228|Summary Ranges|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0228.Summary-Ranges)|42.9%|Easy||
|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 @@
|0813|Largest Sum of Averages||51.3%|Medium||
|0814|Binary Tree Pruning||71.8%|Medium||
|0815|Bus Routes|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0815.Bus-Routes)|43.6%|Hard||
|0816|Ambiguous Coordinates|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0816.Ambiguous-Coordinates)|48.8%|Medium||
|0816|Ambiguous Coordinates|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0816.Ambiguous-Coordinates)|49.0%|Medium||
|0817|Linked List Components|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0817.Linked-List-Components)|57.8%|Medium||
|0818|Race Car||40.6%|Hard||
|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 @@
|1090|Largest Values From Labels||60.2%|Medium||
|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||
|1092|Shortest Common Supersequence||53.4%|Hard||
|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||
|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||
|1094|Car Pooling||59.7%|Medium||
|1095|Find in Mountain Array||36.1%|Hard||
|1096|Brace Expansion II||62.9%|Hard||
@ -1291,7 +1291,7 @@
|1150|Check If a Number Is Majority Element in a Sorted Array||57.0%|Easy||
|1151|Minimum Swaps to Group All 1's Together||58.8%|Medium||
|1152|Analyze User Website Visit Pattern||43.0%|Medium||
|1153|String Transforms Into Another String||35.8%|Hard||
|1153|String Transforms Into Another String||35.7%|Hard||
|1154|Day of the Year|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1154.Day-of-the-Year)|49.2%|Easy||
|1155|Number of Dice Rolls With Target Sum||47.6%|Medium||
|1156|Swap For Longest Repeated Character Substring||47.0%|Medium||
@ -1310,7 +1310,7 @@
|1169|Invalid Transactions||30.7%|Medium||
|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||
|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||
|1172|Dinner Plate Stacks||37.4%|Hard||
|1172|Dinner Plate Stacks||37.5%|Hard||
|1173|Immediate Food Delivery I||82.8%|Easy||
|1174|Immediate Food Delivery II||62.4%|Medium||
|1175|Prime Arrangements|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1175.Prime-Arrangements)|51.7%|Easy||
@ -1436,7 +1436,7 @@
|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||
|1296|Divide Array in Sets of K Consecutive Numbers||55.8%|Medium||
|1297|Maximum Number of Occurrences of a Substring||51.1%|Medium||
|1298|Maximum Candies You Can Get from Boxes||60.1%|Hard||
|1298|Maximum Candies You Can Get from Boxes||60.0%|Hard||
|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||
|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||
|1301|Number of Paths with Max Score||38.3%|Hard||
@ -1531,7 +1531,7 @@
|1390|Four Divisors||39.7%|Medium||
|1391|Check if There is a Valid Path in a Grid||45.2%|Medium||
|1392|Longest Happy Prefix||42.3%|Hard||
|1393|Capital Gain/Loss||91.2%|Medium||
|1393|Capital Gain/Loss||91.1%|Medium||
|1394|Find Lucky Integer in an Array||63.0%|Easy||
|1395|Count Number of Teams||74.1%|Medium||
|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 @@
|1403|Minimum Subsequence in Non-Increasing Order||71.9%|Easy||
|1404|Number of Steps to Reduce a Number in Binary Representation to One||50.1%|Medium||
|1405|Longest Happy String||53.0%|Medium||
|1406|Stone Game III||58.3%|Hard||
|1406|Stone Game III||58.4%|Hard||
|1407|Top Travellers||83.9%|Easy||
|1408|String Matching in an Array||63.7%|Easy||
|1409|Queries on a Permutation With Key||81.9%|Medium||
@ -1883,7 +1883,7 @@
|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||
|1743|Restore the Array From Adjacent Pairs||63.9%|Medium||
|1744|Can You Eat Your Favorite Candy on Your Favorite Day?||30.8%|Medium||
|1745|Palindrome Partitioning IV||49.6%|Hard||
|1745|Palindrome Partitioning IV||49.5%|Hard||
|1746|Maximum Subarray Sum After One Operation||61.8%|Medium||
|1747|Leetflex Banned Accounts||69.2%|Medium||
|1748|Sum of Unique Elements|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1748.Sum-of-Unique-Elements)|74.7%|Easy||

View File

@ -8,7 +8,7 @@ func lengthOfLongestSubstring(s string) int {
var bitSet [256]bool
result, left, right := 0, 0, 0
for left < len(s) {
// 右侧字符对应的bitSet被标记true说明此字符在X位置重复,需要左侧向前移动,直到将X标记为false
// 右侧字符对应的 bitSet 被标记 true说明此字符在 X 位置重复,需要左侧向前移动,直到将 X 标记为 false
if bitSet[s[right]] {
bitSet[s[left]] = false
left++
@ -26,23 +26,28 @@ func lengthOfLongestSubstring(s string) int {
return result
}
// 解法二 滑动窗口-数组桶
// 解法二 滑动窗口
func lengthOfLongestSubstring1(s string) int {
right, left, res := 0, 0, 0
var indexes [256]int
for left < len(s) {
tmp := indexes[s[left]-'a']
if tmp >= right {
right = tmp + 1
}
indexes[s[left]-'a'] = left
left++
res = max(res, left-right)
if len(s) == 0 {
return 0
}
return res
var freq [256]int
result, left, right := 0, 0, -1
for left < len(s) {
if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
freq[s[right+1]-'a']++
right++
} else {
freq[s[left]-'a']--
left++
}
result = max(result, right-left+1)
}
return result
}
// 解法 滑动窗口-哈希桶
// 解法 滑动窗口-哈希桶
func lengthOfLongestSubstring2(s string) int {
right, left, res := 0, 0, 0
indexes := make(map[byte]int, len(s))

View File

@ -51,7 +51,7 @@ func Test_Problem3(t *testing.T) {
for _, q := range qs {
_, p := q.ans3, q.para3
fmt.Printf("【input】:%v 【output】:%v\n", p, lengthOfLongestSubstring_(p.s))
fmt.Printf("【input】:%v 【output】:%v\n", p, lengthOfLongestSubstring(p.s))
}
fmt.Printf("\n\n\n")
}

View File

@ -52,6 +52,7 @@ Explanation: The answer is "wke", with the length of 3.
## 代码
```go
package leetcode
// 解法一 位图
@ -62,7 +63,7 @@ func lengthOfLongestSubstring(s string) int {
var bitSet [256]bool
result, left, right := 0, 0, 0
for left < len(s) {
// 右侧字符对应的bitSet被标记true说明此字符在X位置重复,需要左侧向前移动,直到将X标记为false
// 右侧字符对应的 bitSet 被标记 true说明此字符在 X 位置重复,需要左侧向前移动,直到将 X 标记为 false
if bitSet[s[right]] {
bitSet[s[left]] = false
left++
@ -80,23 +81,28 @@ func lengthOfLongestSubstring(s string) int {
return result
}
// 解法二 滑动窗口-数组桶
// 解法二 滑动窗口
func lengthOfLongestSubstring1(s string) int {
right, left, res := 0, 0, 0
var indexes [256]int
for left < len(s) {
tmp := indexes[s[left]-'a']
if tmp >= right {
right = tmp + 1
}
indexes[s[left]-'a'] = left
left++
res = max(res, left-right)
if len(s) == 0 {
return 0
}
return res
var freq [256]int
result, left, right := 0, 0, -1
for left < len(s) {
if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
freq[s[right+1]-'a']++
right++
} else {
freq[s[left]-'a']--
left++
}
result = max(result, right-left+1)
}
return result
}
// 解法 滑动窗口-哈希桶
// 解法 滑动窗口-哈希桶
func lengthOfLongestSubstring2(s string) int {
right, left, res := 0, 0, 0
indexes := make(map[byte]int, len(s))
@ -117,6 +123,7 @@ func max(a int, b int) int {
}
return b
}
```

View File

@ -61,7 +61,7 @@ weight: 12
|0836|Rectangle Overlap|[Go]({{< relref "/ChapterFour/0800~0899/0836.Rectangle-Overlap.md" >}})|Easy||||43.3%|
|0869|Reordered Power of 2|[Go]({{< relref "/ChapterFour/0800~0899/0869.Reordered-Power-of-2.md" >}})|Medium||||61.3%|
|0878|Nth Magical Number|[Go]({{< relref "/ChapterFour/0800~0899/0878.Nth-Magical-Number.md" >}})|Hard||||28.9%|
|0885|Spiral Matrix III|[Go]({{< relref "/ChapterFour/0800~0899/0885.Spiral-Matrix-III.md" >}})|Medium| O(n^2)| O(1)||71.1%|
|0885|Spiral Matrix III|[Go]({{< relref "/ChapterFour/0800~0899/0885.Spiral-Matrix-III.md" >}})|Medium| O(n^2)| O(1)||71.2%|
|0887|Super Egg Drop|[Go]({{< relref "/ChapterFour/0800~0899/0887.Super-Egg-Drop.md" >}})|Hard||||27.0%|
|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%|
|0892|Surface Area of 3D Shapes|[Go]({{< relref "/ChapterFour/0800~0899/0892.Surface-Area-of-3D-Shapes.md" >}})|Easy||||60.0%|

View File

@ -46,7 +46,7 @@ weight: 14
|1054|Distant Barcodes|[Go]({{< relref "/ChapterFour/1000~1099/1054.Distant-Barcodes.md" >}})|Medium| O(n log n)| O(log n) |❤️|44.4%|
|1122|Relative Sort Array|[Go]({{< relref "/ChapterFour/1100~1199/1122.Relative-Sort-Array.md" >}})|Easy||||68.1%|
|1235|Maximum Profit in Job Scheduling|[Go]({{< relref "/ChapterFour/1200~1299/1235.Maximum-Profit-in-Job-Scheduling.md" >}})|Hard||||47.9%|
|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%|
|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%|
|1329|Sort the Matrix Diagonally|[Go]({{< relref "/ChapterFour/1300~1399/1329.Sort-the-Matrix-Diagonally.md" >}})|Medium||||81.6%|
|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%|
|1636|Sort Array by Increasing Frequency|[Go]({{< relref "/ChapterFour/1600~1699/1636.Sort-Array-by-Increasing-Frequency.md" >}})|Easy||||67.0%|

View File

@ -77,7 +77,7 @@ weight: 6
|1123|Lowest Common Ancestor of Deepest Leaves|[Go]({{< relref "/ChapterFour/1100~1199/1123.Lowest-Common-Ancestor-of-Deepest-Leaves.md" >}})|Medium||||68.2%|
|1145|Binary Tree Coloring Game|[Go]({{< relref "/ChapterFour/1100~1199/1145.Binary-Tree-Coloring-Game.md" >}})|Medium||||51.3%|
|1302|Deepest Leaves Sum|[Go]({{< relref "/ChapterFour/1300~1399/1302.Deepest-Leaves-Sum.md" >}})|Medium||||85.4%|
|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%|
|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%|
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|-------------|-------------|