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

3.Longest Substring without repeating characters

时间:2015-06-02 06:50:27      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

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

 

3.Longest Substring without repeating characters

标签:

原文地址:http://www.cnblogs.com/77rousongpai/p/4545381.html

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