码迷,mamicode.com
首页 > 其他好文 > 详细

*Longest Substring Without Repeating Characters

时间:2015-10-08 06:51:29      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

题目:

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 }

 

*Longest Substring Without Repeating Characters

标签:

原文地址:http://www.cnblogs.com/hygeia/p/4859944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!