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

Leetcode(Longest Substring Without Repeating Characters)

时间:2018-11-02 00:13:24      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:code   png   repeat   out   end   分享   nta   contain   tin   

技术分享图片

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        Set<Character> set = new HashSet<>();
        int ans = 0, i = 0, j = 0;
        while (i < n && j < n) {
            // try to extend the range [i, j]
            if (!set.contains(s.charAt(j))){
                set.add(s.charAt(j++));
                ans = Math.max(ans, j - i);
            }
            else {
                set.remove(s.charAt(i++));
            }
        }
        return ans;
    }
}

Leetcode(Longest Substring Without Repeating Characters)

标签:code   png   repeat   out   end   分享   nta   contain   tin   

原文地址:https://www.cnblogs.com/hugeng007/p/9892836.html

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