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

Longest Substring Without Repeating Characters

时间:2015-05-07 07:36:02      阅读:143      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!