Skip to content

Commit

Permalink
Merge pull request #50 from JessonChan/master
Browse files Browse the repository at this point in the history
Update 3. Longest Substring Without Repeating Characters.go
  • Loading branch information
halfrost committed Aug 19, 2020
2 parents 573f51e + 41d701c commit d0543da
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 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
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,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 d0543da

Please sign in to comment.