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

Longest Substring Without Repeating Characters

时间:2017-07-30 12:47:36      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:char   long   ret   find   return   sts   class   set   for   

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.

 

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

  

Longest Substring Without Repeating Characters

标签:char   long   ret   find   return   sts   class   set   for   

原文地址:http://www.cnblogs.com/apanda009/p/7258501.html

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