程式語言 - LeetCode - Rust - 3. Longest Substring Without Repeating Characters



參考資訊:
https://levelup.gitconnected.com/leetcode-longest-substring-without-repeating-characters-6d15c267582

題目:


解答:

use std::cmp;

impl Solution {
    pub fn length_of_longest_substring(s: String) -> i32 {
        let mut r = 0;
        let mut left = -1;
        let mut hash: Vec<i32> = vec![-1; 128];

        for (pos, ch) in s.char_indices() {
            left = cmp::max(left, hash[ch as usize]);
            hash[ch as usize] = pos as i32;
            r = cmp::max(r, (pos as i32 - left));
        }
        return r;
    }
}