Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
找到不包含重复字母的最长字符串
解题思路:
1.用双指针i,j;其中j往后遍历,thiscount++,当遇到重复的字符时候,j停下
2.用BitMap保存已经遍历过的字符
3.当j停下时候,i往后遍历至重复字符第一次出现的值后面,同时thiscout--
int lengthOfLongestSubstring(char* s) {
int i=,j=;
int flag[]={}; //创建BitMap
int c;
int count=,thiscount=;
while(s[j])
{
c=s[j];
if(!(flag[c>>]&(<<(c&0x1f)))) //当此字符不重复时
{
flag[c>>]|=(<<(c&0x1f)); //标记字符
thiscount++;
if(thiscount>count)
count=thiscount;
}
else
{
while(s[i]!=s[j])
{
c=s[i];
flag[c>>]^=(<<(c&0x1f)); //把重复字符及其第一次出现位置之前的字符从BitMap中删除
thiscount--;
i++;
}
i++;
}
j++;
}
return count;
}