首页 > 其他 > 详细

Longest Substring Without Repeating Characters

时间:2015-05-07 07:33:04      阅读:223      评论:0      收藏:0      [点我收藏+]
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int max = 0;
        
        
        if (s == null || s.length() == 0) {
            return 0;
        }
        
        HashSet<Character> hs = new HashSet<Character>();
        int left = 0;
        
        for (int i = 0; i < s.length(); i++) {
            if (!hs.contains(s.charAt(i))) {
                hs.add(s.charAt(i));
                max = Math.max(max, i - left + 1);
            } else {
                while (left < i && s.charAt(left) != s.charAt(i)) {
                    hs.remove(s.charAt(left));
                    left++;
                }
                left++;
            }
        }
        return max;
    }
}

 

Longest Substring Without Repeating Characters

原文:http://www.cnblogs.com/77rousongpai/p/4483771.html

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