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
| // @Title: 无重复字符的最长子串 (Longest Substring Without Repeating Characters)
// @Author: 15816537946@163.com
// @Date: 2022-01-31 20:18:38
// @Runtime: 4 ms
// @Memory: 2.1 MB
//struct Solution;
use std::cmp::max;
use std::collections::HashMap;
impl Solution {
pub fn length_of_longest_substring(s: String) -> i32 {
let mut ans = 0;
let mut before = -1;
let mut current = 0;
let mut m = HashMap::new();
for c in s.chars() {
if let Some(last) = m.insert(c, current) {
before = max(before, last);
}
ans = max(ans, current - before);
current += 1;
}
ans
}
}
|