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



參考資訊:
https://www.cnblogs.com/grandyang/p/4480780.html

題目:


解答:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int r = 0;
        int left = -1;
        vector<int> hash(128, -1);

        for (int c0 = 0; c0 < s.size(); c0++) {
            left = max(left, hash[s[c0]]);
            hash[s[c0]] = c0;
            r = max(r, (c0 - left));
        }
        return r;
    }
};