LeetCode 03: Longest Substring Without Repeating Characters


Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb"is "abc", which the length is 3. For "bbbbb"the longest substring is "b", with the length of 1.
コードは次のとおりです.
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        bool d[128] = { false };  
		int max_len = 0;  
		int start = 0;
		int size = s.size();
		char c;
		for(int i=0; i<size; i++) {  
			c = s[i];  
			if(!d[c]) {  
				d[c] = true;  
				max_len = (max_len>i-start+1) ? max_len:i-start+1;   
			} else {  
				while(s[start] != c) {  
					d[s[start]] = false;  
					++start;  
				}  
				++start;  
			}  
		}  
		return max_len;  
    }
};