Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 555f734

Browse files
author
Sandy
authored
Merge pull request #83 from openset/develop
Add: longest_substring_without_repeating_characters
2 parents 6d3059f + c93fd34 commit 555f734

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
11
package longest_substring_without_repeating_characters
2+
3+
func lengthOfLongestSubstring(s string) int {
4+
ans, l := 0, len(s)
5+
index := [128]int{}
6+
for i, j := 0, 0; j < l; j++ {
7+
if i < index[s[j]] {
8+
i = index[s[j]]
9+
}
10+
if ans < j-i+1 {
11+
ans = j - i + 1
12+
}
13+
index[s[j]] = j + 1
14+
}
15+
return ans
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
11
package longest_substring_without_repeating_characters
2+
3+
import "testing"
4+
5+
func TestLengthOfLongestSubstring(t *testing.T) {
6+
tests := map[string]int{
7+
"abcabcbb": 3,
8+
"bbbbb": 1,
9+
"pwwkew": 3,
10+
"abcdef": 6,
11+
}
12+
13+
for input, expected := range tests {
14+
output := lengthOfLongestSubstring(input)
15+
if output != expected {
16+
t.Fatalf("input: %v, output: %v, expected: %v", input, output, expected)
17+
}
18+
}
19+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /