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);
    }
};

 

posted @ 2017-06-17 17:51  chengcy  Views(94)  Comments(0Edit  收藏  举报