zoukankan      html  css  js  c++  java
  • 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;
    };
    

      

    学而不思则罔,思而不结则殆,结而不看,一事无成
  • 相关阅读:
    $NOIP2018$赛道修建
    $NOIP2005$过河
    $NOIP2014$飞扬的小鸟
    $[SCOI2014]$方伯伯的玉米田
    大吉大利,晚上吃鸡!
    $HNOI2005$星际贸易
    $CF1142B$ $Lynyrd Skynyrd$
    $SDOI2015$排序
    $NOIP2003$侦探推理
    Build 2016概览
  • 原文地址:https://www.cnblogs.com/windseek/p/8631017.html
Copyright © 2011-2022 走看看