LeetCode # 3 simple questions

Title: Given a string, you find out which does not contain a sub-length of the longest string of repeated characters.

Problem solution: ij were recorded around the boundary of the target string. X on the current character, if there have been earlier, the update was last seen at the left edge of a location, and then update the position of the current character x, traversing the course record at j - i + 1 of the maximum fine.

 

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int ans = 0;
        int len = s.size();
        std::map<int, int> pos;
        for (int i = 0, j = 0; j < len; ++j){
            if (pos.find(s[j]) != pos.end()){
                int left = pos[s[j]];
                i = left > i ? left : i;
            }
            years = (j - i + 1 )> years? (j - i + 1 ) years; 
            pos [s [j]] = j + 1 ; 
        } 
        Return year; 
    } 
};

 

Guess you like

Origin www.cnblogs.com/error408/p/11546510.html