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

    题目:

    Given a string, find the length of the longest substring without repeating characters. For
    example, the longest substring without repeating letters for “abcabcbb” is “abc”, which
    the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1

    解答:

     1 public class Solution {
     2 
     3     public static void main(String[] args) {
     4         String s = "abca";
     5         System.out.println(s);
     6     }
     7 
     8     public int lengthOfLongestSubstring(String s) {
     9         int[] charMap = new int[256];
    10         Arrays.fill(charMap, -1);
    11 
    12         int i = 0;
    13         int maxLen = 0;
    14         for(int j = 0; j < s.length(); j++) {
    15             if(charMap[s.charAt(j)] >= i) {
    16                 i = charMap[s.charAt(j)] + 1;
    17             }
    18 
    19             charMap[s.charAt(j)] = j;
    20             maxLen = Math.max(j-i+1, maxLen);
    21         }
    22 
    23         return maxLen;
    24     }
    25 }

  • 相关阅读:
    xml
    反射
    类加载器
    TCP通信
    UDP通信
    网络通信
    【SpringBoot】Freemarkerの主キー
    【SpringBoot】Warの作成
    【Excel】ファイルの拡張子
    【SAP】TCODE表
  • 原文地址:https://www.cnblogs.com/wylwyl/p/10388840.html
Copyright © 2011-2022 走看看