Longest Substring Without Repeating Characters

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.

https://leetcode.com/problems/longest-substring-without-repeating-characters/

int lengthOfLongestSubstring(char* s) {
    int prev[256];
    int i,j;
    for(i = 0; i < 256; ++i){
        prev[i] = -1;
    }
    int curr_max = 0;
    i = j = 0;
    int len = strlen(s);
    for(; j < len; ++j){
        i = j-i >= j- prev[s[j]] ? prev[s[j]]+1:i;
        curr_max = curr_max < j - i +1 ? j- i+1: curr_max;
        prev[s[j]] = j;
    }
    return curr_max;
}

 

posted @ 2015-04-18 22:10  直方图  阅读(126)  评论(0编辑  收藏  举报