首页 > 代码库 > Leetcode: Longest Substring Without Repeating Characters

Leetcode: Longest Substring Without Repeating Characters

 1 public class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3         int length = s.length();
 4         if (length == 0) return 0;
 5         int begin = 0;
 6         int end = 1;
 7         int longest = 0;
 8         int value = http://www.mamicode.com/0;
 9         Hashtable<Integer, Character> checker = new Hashtable<Integer, Character>();
10         while (end <= length) {
11             String temp = s.substring(begin, end);
12             if (isunique(temp, checker)) {
13                 value = http://www.mamicode.com/(int)(s.charAt(end-1) - ‘\0‘);
14                 checker.put(value, s.charAt(end-1));
15                 end++;
16                 longest++;
17             }
18             else {
19                 checker.remove((int)(s.charAt(begin) - ‘\0‘));
20                 begin++;
21                 end++;
22             }
23         }
24         return longest;
25     }
26     
27     public boolean isunique(String str, Hashtable<Integer, Character> checker) {
28         char c = str.charAt(str.length() - 1);
29         if (checker.containsKey((int) c)) return false;
30         else return true;
31     }
32 }