-
Notifications
You must be signed in to change notification settings - Fork 0
/
p3.rs
35 lines (29 loc) · 1.01 KB
/
p3.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
33
34
35
// https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/
// 维护窗口,只要窗口里有重复字符,移动窗口直至合法
// 用哈希集检查是否有重复元素
pub fn length_of_longest_substring(s: String) -> i32 {
let s: Vec<char> = s.chars().collect();
use std::collections::HashSet;
let mut set: HashSet<char> = HashSet::new();
let mut l: usize = 0;
let mut ans: usize = 0;
for r in 0..s.len() {
while set.contains(&s[r]) {
set.remove(&s[l]);
l += 1;
}
set.insert(s[r]);
ans = ans.max(r - l + 1);
}
ans as i32
}
#[test]
fn test() {
let s1: String = String::from("abcabcbb");
assert_eq!(length_of_longest_substring(s1), 3);
let s2: String = String::from("bbbbb");
assert_eq!(length_of_longest_substring(s2), 1);
let s3: String = String::from("pwwkew");
assert_eq!(length_of_longest_substring(s3), 3);
assert_eq!(length_of_longest_substring("".to_string()), 0);
}