0003.无重复字符的最长子串
方法一:滑动窗口
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
impl Solution {
pub fn length_of_longest_substring(s: String) -> i32 {
use std::cmp::max;
use std::collections::HashSet;
let mut dict = HashSet::new();
let s = s.as_bytes();
let (mut ans, mut i) = (0, 0);
for j in 0..s.len() {
while dict.contains(&s[j]) {
dict.remove(&s[i]);
i += 1;
}
dict.insert(s[j]);
ans = max(ans, (j - i + 1) as i32);
}
ans
}
}