From 3c1176be434fc67b1a7abb4d9e44e86fe283db81 Mon Sep 17 00:00:00 2001 From: novahe Date: Wed, 12 May 2021 00:45:49 +0800 Subject: [PATCH] fix/3: clean up code --- ... Substring Without Repeating Characters.go | 42 ++++++++++------- ...-Substring-Without-Repeating-Characters.md | 47 +++++++++++-------- 2 files changed, 53 insertions(+), 36 deletions(-) diff --git a/leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters.go b/leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters.go index 672dbdb5..a80362d5 100644 --- a/leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters.go +++ b/leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters.go @@ -26,25 +26,35 @@ func lengthOfLongestSubstring(s string) int { return result } -// 解法二 滑动窗口 -func lengthOfLongestSubstring_(s string) int { - if len(s) == 0 { - return 0 - } - var freq [256]int - result, left, right := 0, 0, -1 - +// 解法二 滑动窗口-数组桶 +func lengthOfLongestSubstring1(s string) int { + right, left, res := 0, 0, 0 + var indexes [256]int for left < len(s) { - if right+1 < len(s) && freq[s[right+1]-'a'] == 0 { - freq[s[right+1]-'a']++ - right++ - } else { - freq[s[left]-'a']-- - left++ + tmp := indexes[s[left]-'a'] + if tmp >= right { + right = tmp + 1 } - result = max(result, right-left+1) + indexes[s[left]-'a'] = left + left++ + res = max(res, left-right) } - return result + return res +} + +// 解法二 滑动窗口-哈希桶 +func lengthOfLongestSubstring2(s string) int { + right, left, res := 0, 0, 0 + indexes := make(map[byte]int, len(s)) + for left < len(s) { + if idx, ok := indexes[s[left]]; ok && idx >= right { + right = idx + 1 + } + indexes[s[left]] = left + left++ + res = max(res, left-right) + } + return res } func max(a int, b int) int { diff --git a/website/content/ChapterFour/0001~0099/0003.Longest-Substring-Without-Repeating-Characters.md b/website/content/ChapterFour/0001~0099/0003.Longest-Substring-Without-Repeating-Characters.md index d5406eff..5582a762 100644 --- a/website/content/ChapterFour/0001~0099/0003.Longest-Substring-Without-Repeating-Characters.md +++ b/website/content/ChapterFour/0001~0099/0003.Longest-Substring-Without-Repeating-Characters.md @@ -52,7 +52,6 @@ Explanation: The answer is "wke", with the length of 3. ## 代码 ```go - package leetcode // 解法一 位图 @@ -63,7 +62,7 @@ func lengthOfLongestSubstring(s string) int { var bitSet [256]bool result, left, right := 0, 0, 0 for left < len(s) { - // 右侧字符对应的 bitSet 被标记 true,说明此字符在 X 位置重复,需要左侧向前移动,直到将X标记为 false + // 右侧字符对应的bitSet被标记true,说明此字符在X位置重复,需要左侧向前移动,直到将X标记为false if bitSet[s[right]] { bitSet[s[left]] = false left++ @@ -81,25 +80,35 @@ func lengthOfLongestSubstring(s string) int { return result } -// 解法二 滑动窗口 -func lengthOfLongestSubstring_(s string) int { - if len(s) == 0 { - return 0 - } - var freq [256]int - result, left, right := 0, 0, -1 - +// 解法二 滑动窗口-数组桶 +func lengthOfLongestSubstring1(s string) int { + right, left, res := 0, 0, 0 + var indexes [256]int for left < len(s) { - if right+1 < len(s) && freq[s[right+1]-'a'] == 0 { - freq[s[right+1]-'a']++ - right++ - } else { - freq[s[left]-'a']-- - left++ + tmp := indexes[s[left]-'a'] + if tmp >= right { + right = tmp + 1 } - result = max(result, right-left+1) + indexes[s[left]-'a'] = left + left++ + res = max(res, left-right) } - return result + return res +} + +// 解法二 滑动窗口-哈希桶 +func lengthOfLongestSubstring2(s string) int { + right, left, res := 0, 0, 0 + indexes := make(map[byte]int, len(s)) + for left < len(s) { + if idx, ok := indexes[s[left]]; ok && idx >= right { + right = idx + 1 + } + indexes[s[left]] = left + left++ + res = max(res, left-right) + } + return res } func max(a int, b int) int { @@ -108,8 +117,6 @@ func max(a int, b int) int { } return b } - - ```