-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwtcaE1.rs
32 lines (26 loc) · 1.03 KB
/
wtcaE1.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {
assert_eq!(Solution::length_of_longest_substring("abcabcbb".into()), 3);
assert_eq!(Solution::length_of_longest_substring("bbbbb".into()), 1);
assert_eq!(Solution::length_of_longest_substring("pwwkew".into()), 3);
}
struct Solution;
impl Solution {
/// 使用set来判断是否已经存在,如果存在的话,移动p直到把重复的元素移除为止
/// abcabcbb
/// 如果当前下标为4,p需要移动到2,也就是把重复的b字符移除为止,这时就不包含重复的字符了。
pub fn length_of_longest_substring(s: String) -> i32 {
let mut set = std::collections::HashSet::new();
let mut ans = 0;
let mut p = 0;
for i in 0..s.len() {
while set.contains(&(s.as_bytes()[i])) {
set.remove(&(s.as_bytes()[p]));
p += 1;
}
set.insert(s.as_bytes()[i]);
ans = ans.max(i - p + 1);
}
ans as i32
}
}