首页 > 其他 > 详细

leetcode--Longest Substring Without Repeating Characters

时间:2014-01-31 13:49:59      阅读:362      评论: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.

 

bubuko.com,布布扣
 1 public class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3         int len = 0;
 4         HashSet<Character> hset = new HashSet<Character>(); 
 5         if(s.length() > 0){
 6             StringBuffer temp = new StringBuffer();
 7             temp.append(s.charAt(0));
 8             hset.add(s.charAt(0));
 9             ++len;
10             for(int i = 1; i < s.length(); ++i){
11                 if(hset.contains(s.charAt(i))){
12                     StringBuffer abf = new StringBuffer();
13                     abf.append(s.charAt(i));                    
14                     int index = temp.indexOf(abf.toString());
15                     temp.delete(0, index + 1);
16                 }
17                 else
18                     hset.add(s.charAt(i));                
19                 temp.append(s.charAt(i));
20                 if(temp.length() > len)
21                     len = temp.length();
22             }            
23         }
24         return len;
25     }
26 }
bubuko.com,布布扣

leetcode--Longest Substring Without Repeating Characters

原文:http://www.cnblogs.com/averillzheng/p/3536686.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!