LeetCode 20200312 (without repeating the longest substring of characters)

1. No repeating characters longest substring
This question hashmap need to use a hash table
using hash table to record the final position of each character corresponding to the method
(1) should be revisited to determine if this value already exists in the table without duplication the left end point
(2) to update the last position of the table
(3) further down to the non-repeating length calculated taking 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;
    }
};
Published 60 original articles · won praise 9 · views 3940

Guess you like

Origin blog.csdn.net/puying1/article/details/104820928