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) { if(s.length()==0)return 0; int p1=0, p2=0; int maxlen=0; bool isExisted[256]; memset(isExisted, false, 256); while(p2<s.length()){ if(isExisted[s[p2]]){ maxlen=max(p2-p1, maxlen); while(p1<p2&&s[p1]!=s[p2]){ isExisted[s[p1]]=false; p1++; } isExisted[s[p1]]=false; p1++; } else{ isExisted[s[p2]]=true; p2++; } } maxlen=max(p2-p1, maxlen); //别漏了最后一个子串 return maxlen; } };
LeetCode-003 Longest Substring Without Repeating Characters,布布扣,bubuko.com
LeetCode-003 Longest Substring Without Repeating Characters
原文:http://blog.csdn.net/harryhuang1990/article/details/25719531