Skip to content

Commit bf40f42

Browse files
committed
Update solution 3
1 parent 4c3088b commit bf40f42

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ func lengthOfLongestSubstring1(s string) int {
3131
if len(s) == 0 {
3232
return 0
3333
}
34-
var freq [256]int
34+
var freq [127]int
3535
result, left, right := 0, 0, -1
3636

3737
for left < len(s) {
38-
if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
39-
freq[s[right+1]-'a']++
38+
if right+1 < len(s) && freq[s[right+1]] == 0 {
39+
freq[s[right+1]]++
4040
right++
41+
4142
} else {
42-
freq[s[left]-'a']--
43+
freq[s[left]]--
4344
left++
4445
}
4546
result = max(result, right-left+1)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,16 @@ func lengthOfLongestSubstring1(s string) int {
8686
if len(s) == 0 {
8787
return 0
8888
}
89-
var freq [256]int
89+
var freq [127]int
9090
result, left, right := 0, 0, -1
9191

9292
for left < len(s) {
93-
if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
94-
freq[s[right+1]-'a']++
93+
if right+1 < len(s) && freq[s[right+1]] == 0 {
94+
freq[s[right+1]]++
9595
right++
96+
9697
} else {
97-
freq[s[left]-'a']--
98+
freq[s[left]]--
9899
left++
99100
}
100101
result = max(result, right-left+1)

0 commit comments

Comments
 (0)