*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.

HashSet:

set.contains()

set.remove()

set.add()

 1 public class Solution {
 2     public int lengthOfLongestSubstring(String s) 
 3     {
 4         if(s==null||s.length()==0) return 0;
 5         HashSet<Character> set = new HashSet<Character>();
 6         
 7         int leftbound=0;
 8         int maxlen=0;
 9         for(int i=0;i<s.length();i++)
10         {
11             
12             if(set.contains(s.charAt(i)))
13             {
14                 while(leftbound<i&&s.charAt(leftbound)!=s.charAt(i))
15                 {
16                     set.remove(s.charAt(leftbound));
17                     leftbound++;
18                 }
19                 leftbound++;
20             }
21             else
22             {
23                 set.add(s.charAt(i));
24                 maxlen=Math.max(maxlen,i-leftbound+1);
25             }
26             
27         
28         }
29         return maxlen;
30 
31         
32     }
33 }
原文地址:https://www.cnblogs.com/hygeia/p/4859944.html