Longest Substring Without Repeating Characters
The test set also includes the non-alphabet characters, thus the ASCII_MAX should be defined as 256, and s[i] - 'a' is not necessary. The current code can not pass the OJ. I made minor changes as below with accepted:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
const int ASCII_MAX= 256;
int last_pos[ASCII_MAX];
fill(last_pos, last_pos+ASCII_MAX, -1);
int max_len = 0, len = 0;
for(int i=0; i<s.size(); i++,len++){
if(last_pos[s[i]] >= 0){//find a duplicated char
max_len = max(len, max_len);
i = last_pos[s[i]] + 1;//start from the next possible solution
len = 0;
fill(last_pos, last_pos+ASCII_MAX, -1);
}
last_pos[s[i]] = i;
}
max_len = max(len, max_len);
return max_len;
}
};
感谢提醒,我这个代码的确不能AC了,我会马上根据你的建议进行修改