每日一题2

给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。

方法一:滑动窗口,用一个哈希表记录窗口中的元素。

class Solution {

public:

    int lengthOfLongestSubstring(string s) {

        unordered_map map1;

        int n = s.size();

        int rk = -1;

        int ans =0;

        for(int i =0;i

            if(i!=0){

                map1.erase(s[i-1]);

            }

            while(!map1.count(s[rk+1])&&rk+1

                map1.insert(s[rk+1]);

                rk++;

            }

            ans = max(ans,rk+1-i);

        }

        return ans;

    }

};

你可能感兴趣的:(哈希表,每日一题,蓝桥杯,linq,p2p)