一,Longest Substring Without Repeating Characters
题目描述
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.
我的代码
int lengthOfLongestSubstring(char* s) {
char ch;
int i;
int n = strlen(s);
int a[n];
int max;
i = 0;
max = 0;
ch = s[i];
while(ch != '\0'){
if(i == 0){
max = 1;
a[0] = 1;
} else {
int len = a[i - 1];
int count = 1;
for(int j = i - 1; j >= (i - len); j --){
if(s[j] == ch){
break;
}
count ++;
}
a[i] = count;
if(count > max){
max = count;
}
}
i ++;
ch = s[i];
}
return max;
}