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[] map = new int[128];
    int max = 0, j = 0;
    char[] str = s.toCharArray();
    int length = s.length();

    for(int i = 0; i < length; i++) {
        if(map[str[i]] > 0)
            j =  Math.max(j, map[str[i]]);
        map[str[i]] = i + 1;
        max = Math.max(max, i - j + 1);
    }
    return max;


posted @ 2017-03-07 20:26  壹言  阅读(100)  评论(0编辑  收藏  举报