LeetCode 20200312(无重复字符的最长子串)

1.无重复字符的最长子串
这道题需要用到哈希表hashmap
用哈希表来记录每个字符对应法的最后位置
(1)如果表中已经存在这个值 那么就应该重新确定无重复的左端点
(2)更新表中的最后位置
(3)再下来对无重复的长度进行计算 取max

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int res=0;
        int left=-1;
        unordered_map<int,int>map;
        int len=s.size();
        for(int i=0;i<len;i++){
            if(map.count(s[i])&&map[s[i]]>left){
                left=map[s[i]];
            }
            map[s[i]]=i;
            res=max(res,i-left);

        }
        return res;
    }
};
发布了60 篇原创文章 · 获赞 9 · 访问量 3940

猜你喜欢

转载自blog.csdn.net/puying1/article/details/104820928