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

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.

/**
 * @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;
};

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏计算机视觉与深度学习基础

Leetcode 208 Implement Trie (Prefix Tree) 字典树

Implement a trie with insert, search, and startsWith methods. Note: You may ...

1836
来自专栏小樱的经验随笔

Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3)(A.B.C,3道暴力题,C可二分求解)

A. Is it rated? time limit per test:2 seconds memory limit per test:256 megabyte...

3157
来自专栏ml

hduoj------确定比赛名次

拓扑排序结合代码的完整理解 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Li...

2567
来自专栏用户画像

5.2.3 十字链表

十字链表是有向图的一种链式存储结构。在十字链表中,对应于有向图中的每条弧有一个结点,对应于每个顶点也有一个结点,这些结点的结构如下:

632
来自专栏算法修养

CodeForces 708B Recover the String

B. Recover the String time limit per test 1 second memory limit per test 2...

31810
来自专栏wym

Codeforces Round #483 (Div. 2) A. Game

Initially there are nn integers a1,a2,…,ana1,a2,…,an written on the board. Each ...

832
来自专栏专知

【LeetCode 459】关关的刷题日记42 – Repeated Substring Pattern

关关的刷题日记42 – Leetcode 459. Repeated Substring Pattern 题目 Given a non-empty string...

3436
来自专栏小樱的经验随笔

UVA 11039-Building designing【贪心+绝对值排序】 UVA11039-Building designing

UVA11039-Building designing Time limit: 3.000 seconds An architect wants to desi...

28710
来自专栏算法修养

POJ--1050--To the Max(线性动规,最大子矩阵和)

To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4...

3245
来自专栏机器学习入门

LWC 58:724. Find Pivot Index

LWC 58:724. Find Pivot Index 传送门:724. Find Pivot Index Problem: Given an array ...

1798

扫码关注云+社区