最长无重复字母子串
using namespace std;
class solution{
public:
int lengthOfLongestSubstring(string s)
{
int freq[256] ={0};
int l = 0; r = -1; //滑动窗口为s[l....r]
int res = 0;
while(l<s.size())
{
if(r + 1 < s.size() && freq[s[r+1]]==0)
freq[s[++r]] ++;
else
freq[s[l++]] --;
res = max(res, r-l+1);
}
return res;
}
};
leetcode438
public:
vector<int> findAnagrams(string s, string p) {
vector<int> ans;
if(p.empty() || s.empty()) return ans;
int cnt[256] = {0};
for(char ch : p) cnt[ch] ++;
int lf = 0, rt = 0;
while(rt < s.size()) {
cnt[s[rt]] --;
while(lf <= rt && cnt[s[rt]] < 0) {
cnt[s[lf ++]] ++;
}
if(rt - lf + 1 == p.size()) {
ans.push_back(lf);
cnt[s[lf ++]] ++;
}
rt ++;
}
return ans;
}











网友评论