前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetCode刷题(找到最长的连续不重复的字符串长度)

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

作者头像
windseek
发布2018-06-14 17:39:42
1.2K0
发布2018-06-14 17:39:42
举报
文章被收录于专栏:杨龙飞前端杨龙飞前端

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 subsequenceand not a substring.

代码语言:javascript
复制
/**
 * @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;
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档