mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-08-05 21:20:59 +08:00
Optimized solution 3、39
This commit is contained in:
@ -55,13 +55,36 @@ Explanation: The answer is "wke", with the length of 3.
|
||||
|
||||
package leetcode
|
||||
|
||||
// 解法一 位图
|
||||
func lengthOfLongestSubstring(s string) int {
|
||||
if len(s) == 0 {
|
||||
return 0
|
||||
}
|
||||
// 扩展 ASCII 码的位图表示(BitSet),共有 256 位
|
||||
var bitSet [256]uint8
|
||||
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
|
||||
left++
|
||||
}
|
||||
result = max(result, right-left)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// 解法二 滑动窗口
|
||||
func lengthOfLongestSubstring_(s string) int {
|
||||
if len(s) == 0 {
|
||||
return 0
|
||||
}
|
||||
var freq [256]int
|
||||
result := 0
|
||||
left, right := 0, -1
|
||||
result, left, right := 0, 0, -1
|
||||
|
||||
for left < len(s) {
|
||||
if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
|
||||
@ -76,6 +99,14 @@ func lengthOfLongestSubstring(s string) int {
|
||||
return result
|
||||
}
|
||||
|
||||
func max(a int, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user