Skip to content

Commit

Permalink
Update 3. Longest Substring Without Repeating Characters.go
Browse files Browse the repository at this point in the history
improve
  • Loading branch information
JessonChan authored Aug 18, 2020
1 parent 7989003 commit 41d701c
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ func lengthOfLongestSubstring(s string) int {
if len(s) == 0 {
return 0
}
// 扩展 ASCII 码的位图表示(BitSet),共有 256 位
var bitSet [256]uint8
var bitSet [256]bool
result, left, right := 0, 0, 0
for left < len(s) {
if right < len(s) && bitSet[s[right]] == 0 {
// 标记对应的 ASCII 码为 1
bitSet[s[right]] = 1
right++
} else {
// 标记对应的 ASCII 码为 0
bitSet[s[left]] = 0
// 右侧字符对应的bitSet被标记true,说明此字符在X位置重复,需要左侧向前移动,直到将X标记为false
if bitSet[s[right]] {
bitSet[s[left]] = false
left++
} else {
bitSet[s[right]] = true
right++
}
if result < right-left {
result = right - left
}
if left+result >= len(s) || right >= len(s) {
break
}
result = max(result, right-left)
}
return result
}
Expand Down

0 comments on commit 41d701c

Please sign in to comment.