Skip to content

Commit 48f079b

Browse files
committed
0003
1 parent 72522d4 commit 48f079b

File tree

3 files changed

+47
-49
lines changed

3 files changed

+47
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,30 @@
11
use std::cmp::max;
2+
use std::collections::HashMap;
3+
4+
pub struct Solution {}
25

36
impl Solution {
47
pub fn length_of_longest_substring(s: String) -> i32 {
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;
8+
let mut index = 0;
9+
let mut prev_index = -1;
10+
let mut result = 0;
11+
let mut sub_strings: HashMap<char, i32> = HashMap::new();
12+
13+
for c in s.chars() {
14+
match sub_strings.get(&c) {
15+
None => {}
16+
Some(p_ind) => {
17+
if prev_index < *p_ind {
18+
prev_index = *p_ind;
19+
}
20+
}
2521
}
26-
i += 1;
27-
}
2822

29-
if loop_finish {
30-
sub_strings.push(c);
23+
result = max(result, index - prev_index);
24+
sub_strings.insert(c, index);
25+
index += 1;
3126
}
3227

33-
end += 1;
34-
result = max(result, end - start);
28+
return result as i32;
3529
}
36-
37-
return result as i32;
38-
}
39-
}
40-
41-
42-
43-
fn main() {
44-
let s = String::from("Hello world");
45-
let result = longest_substring_without_repeating_characters(s);
46-
47-
println!("{}", result);
4830
}

0 commit comments

Comments
 (0)