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

Longest Substring Without Repeating Characters

时间:2015-04-20 13:07:35      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

经典问题,可以把周边都做了,详见ref  http://blog.csdn.net/linhuanmars/article/details/19949159

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s==null||s.length()<1) return 0;
        int w =0, r = 0, len = 1;
        HashSet<Character> hs = new HashSet<Character>();
        while(r<s.length()){
            if(hs.contains(s.charAt(r))){
                len = Math.max(len, r-w);
                while(s.charAt(w)!=s.charAt(r)){
                    hs.remove(s.charAt(w)); // !!!!
                    w++;
                }
                w++; ///!!!
            }else{
                hs.add(s.charAt(r));
            }  
            r++;
        }
        return Math.max(len,r-w);
    }
}

 

Longest Substring Without Repeating Characters

标签:

原文地址:http://www.cnblogs.com/jiajiaxingxing/p/4441211.html

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