We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 853e330 commit 4ff94a7Copy full SHA for 4ff94a7
leet_code/lengthOfLongestSubstring_test.go
@@ -5,18 +5,23 @@ import (
5
)
6
7
func TestLengthOfLongestSubstring(t *testing.T) {
8
- t.Log(lengthOfLongestSubstring("abcabcbb"))
9
-}
10
-
11
-type Data struct {
12
- Index int
13
- Val int
14
- have bool
+ t.Log(lengthOfLongestSubstring("abcdefgafkzwb"))
15
}
16
17
func lengthOfLongestSubstring(s string) int {
18
- if len(s) == 0 {
19
- return 0
+ lastOccurred := make(map[byte]int)
+ start := 0
+ maxLength := 0
+ for i, ch := range []byte(s) {
+ //找到上次该字母出现的起点
+ //判断该位置是否大于起点
+ if lastI, ok := lastOccurred[ch]; ok && lastI >= start {
+ start = lastI + 1 //从该字母后一位作为起点,以前的存在相同的而且已经判断就不再判断
20
+ }
21
+ if i-start+1 > maxLength {
22
+ maxLength = i - start + 1
23
24
+ lastOccurred[ch] = i
25
26
+ return maxLength
27
0 commit comments