Skip to content

Commit 3c1176b

Browse files
committed
fix/3: clean up code
1 parent 012999a commit 3c1176b

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,35 @@ func lengthOfLongestSubstring(s string) int {
2626
return result
2727
}
2828

29-
// 解法二 滑动窗口
30-
func lengthOfLongestSubstring_(s string) int {
31-
if len(s) == 0 {
32-
return 0
29+
// 解法二 滑动窗口-数组桶
30+
func lengthOfLongestSubstring1(s string) int {
31+
right, left, res := 0, 0, 0
32+
var indexes [256]int
33+
for left < len(s) {
34+
tmp := indexes[s[left]-'a']
35+
if tmp >= right {
36+
right = tmp + 1
37+
}
38+
indexes[s[left]-'a'] = left
39+
left++
40+
res = max(res, left-right)
3341
}
34-
var freq [256]int
35-
result, left, right := 0, 0, -1
42+
return res
43+
}
3644

45+
// 解法二 滑动窗口-哈希桶
46+
func lengthOfLongestSubstring2(s string) int {
47+
right, left, res := 0, 0, 0
48+
indexes := make(map[byte]int, len(s))
3749
for left < len(s) {
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++
50+
if idx, ok := indexes[s[left]]; ok && idx >= right {
51+
right = idx + 1
4452
}
45-
result = max(result, right-left+1)
53+
indexes[s[left]] = left
54+
left++
55+
res = max(res, left-right)
4656
}
47-
return result
57+
return res
4858
}
4959

5060
func max(a int, b int) int {

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

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

5454
```go
55-
5655
package leetcode
5756

5857
// 解法一 位图
@@ -63,7 +62,7 @@ func lengthOfLongestSubstring(s string) int {
6362
var bitSet [256]bool
6463
result, left, right := 0, 0, 0
6564
for left < len(s) {
66-
// 右侧字符对应的 bitSet 被标记 true,说明此字符在 X 位置重复,需要左侧向前移动,直到将X标记为 false
65+
// 右侧字符对应的bitSet被标记true,说明此字符在X位置重复,需要左侧向前移动,直到将X标记为false
6766
if bitSet[s[right]] {
6867
bitSet[s[left]] = false
6968
left++
@@ -81,25 +80,35 @@ func lengthOfLongestSubstring(s string) int {
8180
return result
8281
}
8382

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

99+
// 解法二 滑动窗口-哈希桶
100+
func lengthOfLongestSubstring2(s string) int {
101+
right, left, res := 0, 0, 0
102+
indexes := make(map[byte]int, len(s))
92103
for left < len(s) {
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++
104+
if idx, ok := indexes[s[left]]; ok && idx >= right {
105+
right = idx + 1
99106
}
100-
result = max(result, right-left+1)
107+
indexes[s[left]] = left
108+
left++
109+
res = max(res, left-right)
101110
}
102-
return result
111+
return res
103112
}
104113

105114
func max(a int, b int) int {
@@ -108,8 +117,6 @@ func max(a int, b int) int {
108117
}
109118
return b
110119
}
111-
112-
113120
```
114121

115122

0 commit comments

Comments
 (0)