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 };
     
  • 相关阅读:
    YAR 并行RPC框架研究
    Yar
    Monolog
    laravel controller:make
    eclipse自动补全的设置(自动提示)
    如何在 PHP 中处理 Protocol Buffers 数据
    JAVA printWriter中write()和println()区别
    eclipse中启动tomcat
    Ajax简介
    div
  • 原文地址:https://www.cnblogs.com/huenchao/p/7630381.html
Copyright © 2011-2022 走看看