3. Longest Substring Without Repeating Characters (ASCII码128个,建立哈西表)

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.

int lengthOfLongestSubstring(char* s) {
int latestPos[]; //map<char,int>
int result = ;
int i = ;
int startPos = ; if(strcmp(s,"")==) return result; memset(latestPos, -, sizeof(latestPos));
latestPos[s[]] = ;
while(s[i]!='\0'){
if(latestPos[s[i]] >= startPos){ //s[i] already appeared
if(i-startPos > result) result = i-startPos;
startPos = latestPos[s[i]]+;
}
latestPos[s[i]] = i;
i++;
}
if(i-startPos > result) result = i-startPos;
return result;
}
上一篇:maven 项目 编码


下一篇:Cows - POJ 3348(凸包求面积)