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 }

  • 相关阅读:
    2021.3.3
    2021.3.2
    2021.3.1
    2021.2.28(每周总结)
    2021.2.27
    2021.2.26
    2021.2.25
    2021.2.23
    Redis系统学习之五大基本数据类型(List(列表))
    Redis系统学习之五大基本数据类型(String(字符串))
  • 原文地址:https://www.cnblogs.com/wylwyl/p/10388840.html
Copyright © 2011-2022 走看看