Skip to content

Commit 572aa2e

Browse files
committed
0003
1 parent 5105fdb commit 572aa2e

File tree

3 files changed

+46
-83
lines changed

3 files changed

+46
-83
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,45 @@
1-
package problem0003
2-
3-
func lengthOfLongestSubstring(s string) int {
4-
// location[s[i]] == j 表示:
5-
// s中第i个字符串,上次出现在s的j位置,所以,在s[j+1:i]中没有s[i]
6-
// location[s[i]] == -1 表示: s[i] 在s中第一次出现
7-
location := [256]int{} // 只有256长是因为,假定输入的字符串只有ASCII字符
8-
for i := range location {
9-
location[i] = -1 // 先设置所有的字符都没有见过
10-
}
11-
12-
maxLen, left := 0, 0
13-
14-
for i := 0; i < len(s); i++ {
15-
// 说明s[i]已经在s[left:i+1]中重复了
16-
// 并且s[i]上次出现的位置在location[s[i]]
17-
if location[s[i]] >= left {
18-
left = location[s[i]] + 1 // 在s[left:i+1]中去除s[i]字符及其之前的部分
19-
} else if i+1-left > maxLen {
20-
// fmt.Println(s[left:i+1])
21-
maxLen = i + 1 - left
22-
}
23-
location[s[i]] = i
24-
}
25-
26-
return maxLen
1+
use std::cmp::max;
2+
3+
pub fn longest_substring_without_repeating_characters (s: String) -> usize {
4+
5+
let mut start = 0;
6+
let mut end = 0;
7+
8+
let s_length = s.len();
9+
let mut sub_strings: Vec<char> = vec![];
10+
11+
let mut result = 0;
12+
13+
while end < s_length {
14+
let c = s.chars().nth(end).unwrap();
15+
16+
let mut loop_finish = true;
17+
let mut i = 0;
18+
for vv in sub_strings.iter() {
19+
if vv == &c {
20+
start = i + start + 1;
21+
end = start - 1;
22+
loop_finish = false;
23+
sub_strings = vec![];
24+
break;
25+
}
26+
i += 1;
27+
}
28+
29+
if loop_finish {
30+
sub_strings.push(c);
31+
}
32+
33+
end += 1;
34+
result = max(result, end - start);
35+
}
36+
37+
return result;
38+
}
39+
40+
fn main() {
41+
let s = String::from("Hello world");
42+
let result = longest_substring_without_repeating_characters(s);
43+
44+
println!("{}", result);
2745
}
Original file line numberDiff line numberDiff line change
@@ -1,56 +0,0 @@
1-
package problem0003
2-
3-
import (
4-
"testing"
5-
6-
"github.com/stretchr/testify/assert"
7-
)
8-
9-
type para struct {
10-
one string
11-
}
12-
13-
type ans struct {
14-
one int
15-
}
16-
17-
type question struct {
18-
p para
19-
a ans
20-
}
21-
22-
func Test_OK(t *testing.T) {
23-
ast := assert.New(t)
24-
25-
qs := []question{
26-
question{
27-
p: para{
28-
one: "abcabcbb",
29-
},
30-
a: ans{
31-
one: 3,
32-
},
33-
},
34-
question{
35-
p: para{
36-
one: "bbbbbbbb",
37-
},
38-
a: ans{
39-
one: 1,
40-
},
41-
},
42-
question{
43-
p: para{
44-
one: "pwwkew",
45-
},
46-
a: ans{
47-
one: 3,
48-
},
49-
},
50-
}
51-
52-
for _, q := range qs {
53-
a, p := q.a, q.p
54-
ast.Equal(a.one, lengthOfLongestSubstring(p.one), "输入:%v", p)
55-
}
56-
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ leetcode in rust lang.
1414
|order|topic|passing rate|difficult|collection|
1515
|:-:|:-|:-:|:-:|:-:|
1616
|1|[Two Sum](./Algorithms/0001.two-sum)|39%|Easy||
17-
|2|[Add Two Numbers](./Algorithms/0002.add-two-numbers)|29%|Medium||
17+
|2|[Add Two Numbers](./Algorithms/0002.add-two-numbers)|29%|Medium||
18+
|3|[Longest Substring Without Repeating Characters](./Algorithms/0003.longest-substring-without-repeating-characters)|25%|Medium||

0 commit comments

Comments
 (0)