zoukankan      html  css  js  c++  java
  • 3. Longest Substring Without Repeating Characters

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

     1 /**
     2  * @param {string} s
     3  * @return {number}
     4  */
     5 var lengthOfLongestSubstring = function(s) {
     6 
     7     var len = s.length,
     8         local =1;
     9         max = 1,
    10         start = 0;
    11 
    12     //处理"";
    13     if(!len){
    14         return 0;
    15     }
    16     
    17     
    18     for(var end = 1;end < len;end++){
    19         
    20         var c = s.charAt(end);
    21         var pattern = s.substring(start,end);
    22         var index = pattern.indexOf(c);
    23         
    24         if(index !== -1){
    25             
    26             local = end - start - index; 
    27             start = start + index + 1;
    28         }else{
    29             local += 1;
    30             max = local > max ? local : max;
    31         }
    32         
    33     }
    34     return max;
    35 };
     
  • 相关阅读:
    docker镜像
    docker常用命令
    docker基础
    跨站脚本漏洞(XSS)基础
    Session、Cookie与Token
    linux之curl工具
    ssl证书与java keytool工具
    mysql主从复制
    linux之平均负载(学习笔记非原创)
    mysql8.0忘记密码如何操作?
  • 原文地址:https://www.cnblogs.com/huenchao/p/7630381.html
Copyright © 2011-2022 走看看