首页 > 其他 > 详细

LeetCode3 无重复的子字符串

时间:2021-05-20 22:04:21      阅读:22      评论:0      收藏:0      [点我收藏+]

方法1:暴力法

方法2:滑动窗口
用set保存已存在的字符,如果end处的字符重复,就将start减1

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int start = 0;
        int end = 0;
        int res = 0;
        Set<Character> set = new HashSet<>();
        while(end<s.length()){
            if(set.contains(s.charAt(end))){
                set.remove(s.charAt(start));
                start++;
            }else{
                set.add(s.charAt(end));
                end++;
                res = Math.max(res,end-start);
            }
        }
        return res;


    }
}

方法3:动态规划

LeetCode3 无重复的子字符串

原文:https://www.cnblogs.com/ermiao-zy/p/14791232.html

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