leetCode刷题(找到最长的连续不重复的字符串长度)


Given a string, find the length of thelongest substringwithout 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 asubstring,"pwke"is asubsequenceand not a substring.

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function(s) {
    //这道题是为了找到最长的连续不重复的字符串长度
    //可以先过滤掉所有的不重复字符串
    var i,j=0;
    var lastStr="";
    var maxLength=0;
    for(i=0;i<s.length;){
        if(lastStr.indexOf(s[i])==-1){
            lastStr=lastStr.concat(s[i++])
            maxLength=Math.max(maxLength,i-j);
        }else{
            lastStr=lastStr.slice(1);
            j++;
        }
    }
    return maxLength;
};
优质内容筛选与推荐>>
1、mac下安装好jdk和jmeter后设置环境变量
2、二分查找----(返回第一个找到的值)
3、.net 应用迁移到Mono 工具 (Moma)
4、CSS3 中的按钮效果与进度条
5、新高考6选3的思考


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号