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.
unsight char 的范围是 0-255.
char 的范围是-128-127.
char *s 表示的是一个指针数组。
int lengthOfLongestSubstring(char* s)
{
bool flag[256];
int max =0;
int len = strlen(s);
int i = 0, j= 0;
for(int m = 0; m < 256 ; m++)
{
flag[m] = false;
}
while((i < len)&&(j < len))
{
if(!flag[s[j]])
{
flag[s[j]] = true;
max = ((j-i+1)>max)?(j-i+1):max;
j++;
}
else
{
flag[s[i]] = false;
i++;
}
}
return max;
}