力扣 class Solution { public: nt lonigestOnes(vector<int>& A, int K) { int left=0; int right=0; int cnt=0; int res=0; while(right<A.size()){ while(right<A.size()){ if(A[right]==0) cnt++; if(cnt>K) break; right++; } res=max(res, right-left); if(A[left]==0) cnt--; left++; right++;// 滑动窗口没必要 减小 // cout<<left<<" "<<right<<endl; } return res; } }; lintcode 1261. 字符至少出现K次的最长子串 class Solution { public: /** * @param s: A string * @param k: An integer * @return: An integer */ int lengthOfLongestSubstringKDistinct(string &s, int k) { // Write your code here map<char,int>mp; int left=0; int right=0; int n=s.size(); int res=0; while(right<n){ while(right<n){ mp[s[right]]++; if(mp.size()>k) break; right++; } res=max(res,right-left); mp[s[left]]--; if(mp[s[left]]==0) mp.erase(s[left]); cout<<left<<" "<<right<<"mp"<<mp.size()<<endl; left++; right++; } return res; } };