程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 3. Longest Substring Without Repeating Characters,longestrepeating

3. Longest Substring Without Repeating Characters,longestrepeating

編輯:JAVA綜合教程

3. Longest Substring Without Repeating Characters,longestrepeating


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 public class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3         int max=0;
 4         char[] ss=s.toCharArray();
 5         
 6         for(int i=0;i<ss.length;i++)
 7         {
 8             int count=0;
 9             Map<Character,Character> map=new HashMap<>();
10             for(int j=i;j<ss.length;j++)
11             {
12                 if(!map.containsKey(ss[j]))
13                 {
14                 map.put(ss[j],ss[j]);
15                 count++;
16                 } 
17                 else break;
18             }
19             if(count>max)
20             max=count;
21         }
22         return max;
23     }
24 }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved