首页 > 代码库 > Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int hashTable[255];
        fill(hashTable, hashTable+255, -1);
        
        int start = 0, maxLen = 0;
        for(int i=0; i<s.size(); ++i)
        {
            if(hashTable[s[i]] >= start)
            {
                maxLen = max(maxLen, i-start);
                start = hashTable[s[i]]+1;
            }
            
            hashTable[s[i]] = i;
        }
        
        return max(maxLen, (int)s.size()-start);
    }
};

 

Longest Substring Without Repeating Characters