Skip to content

Commit fc91870

Browse files
Add Solution 0003 [golang]
1 parent cad42ee commit fc91870

File tree

1 file changed

+22
-0
lines changed
  • solution/0003.Longest Substring Without Repeating Characters

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Report by leetcode.com
3+
* Runtime: 8 ms, Memory Usage: 3.2 MB
4+
*/
5+
func lengthOfLongestSubstring(s string) int {
6+
mathMax := func(a, b int) int {
7+
if a > b {
8+
return a
9+
}
10+
return b
11+
}
12+
cache := map[rune]int{}
13+
var max, position int
14+
for i, r := range s {
15+
if num, ok := cache[r]; ok {
16+
position = mathMax(position, num+1)
17+
}
18+
cache[r] = i
19+
max = mathMax(max, i-position+1)
20+
}
21+
return max
22+
}

0 commit comments

Comments
 (0)