码迷,mamicode.com
首页 > 其他好文 > 详细

Longest Substring Without Repeating Characters

时间:2019-02-16 19:25:38      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:oid   static   ++   col   解答   color   main   character   rac   

题目:

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 }

技术图片

 

Longest Substring Without Repeating Characters

标签:oid   static   ++   col   解答   color   main   character   rac   

原文地址:https://www.cnblogs.com/wylwyl/p/10388840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!