1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// @Title: 无重复字符的最长子串 (Longest Substring Without Repeating Characters)
// @Author: 15816537946@163.com
// @Date: 2020-10-20 23:06:29
// @Runtime: 0 ms
// @Memory: 2 MB
use std::collections::HashMap;
impl Solution {
    pub fn length_of_longest_substring(s: String) -> i32 {
        let mut location: [i32; 256] = [-1; 256];
        let mut left = 0;
        let mut max_len = 0;
        for (i, v) in s.chars().enumerate() {
            if location[v as usize] >= left {
                left = location[v as usize] + 1;
            } else if i - left as usize + 1 > max_len {
                max_len = i - left as usize + 1;
            }
            location[v as usize] = i as i32;
        }
        max_len as i32
    }
}