3. 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) {
    int maxLen = 0,len = 0,list[128],start = 0,asc,i;
    for(i=0;i<128;i++) list[i] = -1;
    for(i=0;s[i] != '\0';i++){
        asc = list[s[i] - '\0'];
        if(list[asc] == -1 || start > list[asc]) len++;
        else{
            if(len > maxLen) maxLen = len;
            len -= list[asc] - start;
            start = list[asc] + 1;
        }
        list[asc] = i;
    }
    if(len > maxLen) maxLen = len;
    return maxLen;
}


posted @ 2018-03-12 08:34  ACLJW  阅读(117)  评论(0编辑  收藏  举报