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     public int lengthOfLongestSubstring(String s) {
 3         // Note: The Solution object is instantiated only once and is reused by each test case.
 4         int matrix = 0;
 5         int header = 0;
 6         int tail = 0;
 7         int length = 0;
 8         int max = 0;
 9         int max_header = 0;
10         int max_tail = 0;
11         while(tail < s.length()){
12             int k = s.charAt(tail) - 'a';
13             if((matrix & (1 << k)) == 0){
14                 matrix = matrix | (1 << k);
15                 tail ++;
16             }
17             else{
18                 length = tail - header;
19                 
20                 if(length > max) {
21                     max = length;
22                     max_header = header;
23                     max_tail = tail;
24                 }
25                 while(s.charAt(header) != s.charAt(tail)){
26                     matrix = matrix & ~(1 << (s.charAt(header) - 'a'));
27                     header ++;
28                 }
29                 header ++;
30                 tail ++;
31             }
32         }
33         length = tail - header;
34         if(length > max) {
35             max = length;
36             max_header = header;
37             max_tail = tail;
38         }
39         return max;
40     }
41 }

 第二遍::

一开始傻呵呵的用了暴力方法,慢的要死;看到提示了知道可以用一个table来存储每一个字符在字符串中的位置。其实这是一个对于字符串问题的通用方法,因为字符本质上就是一个Unique的数字,因此建立一个数组,数组的下标表示这个字符的ASCII码,元素表示其在字符串中的位置即可。另外一个逻辑就是当遇到和之前重复的字符(前一次出现称为Occur 1, 这一次出现称为Occur 2)时,即遇到了一个可能的最大值;进行比较后,将下一次扫描的起点设置为刚才这个重复字符的Occur 1的后一位即可。看到一张图,觉得很不错,转载过来:


 这样其实是桶排序的应用了。使用固定的空间来做。

 1 public class Solution {
 2    public int lengthOfLongestSubstring(String s) {
 3         // Start typing your Java solution below
 4         // DO NOT write main() function
 5         int length = s.length();
 6         if (length == 0) {
 7             return 0;
 8         }
 9         int [] countTable = new int[256];
10         Arrays.fill(countTable, -1);
11         int max = 1;
12         int start = 0;
13         int cur = 1;
14         
15         countTable[s.charAt(0)] = 0;
16         while (cur < length) {
17             //Has not reached a duplicate char
18             if(countTable[s.charAt(cur)] >= start){
19                 max = Math.max(max, cur - start);
20                 start = countTable[s.charAt(cur)] + 1;
21             }
22             countTable[s.charAt(cur)] = cur;
23             cur ++;
24         }
25         max = Math.max(max, cur - start);
26         return max;
27     }
28 }

 第二遍:

 1 public class Solution {
 2    public int lengthOfLongestSubstring(String s) {
 3         // Start typing your Java solution below
 4         // DO NOT write main() function
 5         if(s == null) return 0;
 6         int[] chars = new int[256];
 7         //fill(int[] a, int val)
 8         Arrays.fill(chars, -1);
 9         int start = 0;
10         int max = 0;
11         int i = 0;
12         for(; i < s.length(); i ++){
13             if(chars[s.charAt(i)] >= start){
14                 if(i - start > max) max = i - start;
15                 start = chars[s.charAt(i)] + 1;
16             }
17             chars[s.charAt(i)] = i;
18         }
19         if(i - start > max) max = i - start;
20         return max;
21     }
22 }

 

posted on 2013-03-28 06:42  Step-BY-Step  阅读(277)  评论(0编辑  收藏  举报

导航