首页 > 其他 > 详细

最长不含重复字符的字符号串

时间:2020-07-18 11:32:25      阅读:37      评论:0      收藏:0      [点我收藏+]

技术分享图片
技术分享图片

class Solution {
    public int lengthOfLongestSubstring(String s) {
     if(s.length()==0) return 0;   
//双指针(r指针最先向右移动,遇到重复的l指针向右移动直到越过重复的r指针继续向右移动)
    int l=0,r=0,res=0;
    Map<Character,Integer> map = new HashMap<>();
    while(l<=r){
        char ch_r = s.charAt(r);
        map.put(ch_r,map.getOrDefault(ch_r,0)+1);
        while(map.get(ch_r)>1){
            char ch_l = s.charAt(l);
            map.put(ch_l,map.getOrDefault(ch_l,0)-1);
            l++;
        }
        res = res > r-l+1? res:r-l+1;
        r++;
        if(r==s.length()) break;

    }
    return res;
    }
}

技术分享图片

最长不含重复字符的字符号串

原文:https://www.cnblogs.com/cstdio1/p/13334448.html

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