首页 > 其他 > 详细

LeetCode 3.无重复字符的最长子串

时间:2020-10-25 11:02:53      阅读:30      评论:0      收藏:0      [点我收藏+]

题目思路
双指针算法,只要没有出现重复的字符,就一直将i指针向后移动,如果出现了,就判断重复字符出现的字符是否是1次,然后j指针不断向后走

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<int,int> heap;
        int res = 0;
        for(int i = 0, j = 0;i < s.length();i++)
        {
            heap[s[i]] ++;
            while(heap[s[i]] > 1) heap[s[j ++]]--;
            res = max(res, i - j + 1);
        }
        return res;
    }
};

LeetCode 3.无重复字符的最长子串

原文:https://www.cnblogs.com/zykBlog/p/13871577.html

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