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 public int lengthOfLongestSubstring(String s) { 3 int len = s.length(); 4 if(len<=0) return 0; 5 int c[] = new int [26]; 6 reset(c); 7 int count=0; 8 int max = 0; 9 for(int i=0;i<len;i++,count++){ 10 if(c[s.charAt(i)-‘a‘]!=-1){ 11 i = c[s.charAt(i)-‘a‘]+1; 12 reset(c); 13 max = Math.max(max,count); 14 count = 0; 15 } 16 c[s.charAt(i)-‘a‘] = i; 17 } 18 max = Math.max(max,count); 19 return max; 20 21 } 22 public void reset (int []c){ 23 for(int i=0;i<26;i++){ 24 c[i]=-1; 25 } 26 } 27 }
Longest Substring Without Repeating Characters
原文:http://www.cnblogs.com/krunning/p/3560699.html