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.

大概意思:给出一个字符串找出最长的子串,子串中不能有重复的字符。

说实话因为英文较差,刚开始的时候连题目都没弄懂 囧!,最后找了网上的java来改的。

var lengthSubstringLength=function(s){
        var length=s.length;
        var j=0,i=0;
        var maxlength=0;
        var curlength=0;
        var exist=new Array();
        while(j<length){
            if(!exist[s[j]]){
                exist[s[j]]=true;
                j++;
            }else{
                while(s[i]!=s[j]){
                    exist[s[i]]=false;
                    i++;
                }
                i++;
                j++;
            }               
            curlength=j-i;
            maxlength=maxlength>curlength?maxlength:curlength;
        }
    return maxlength;
}       

var stringLength=lengthSubstringLength('abcddefa');
console.log(stringLength);