找到字符串中所有字母异位词 - C++力扣438题
题目链接:https://leetcode.com/problems/find-all-anagrams-in-a-string/description/
解题思路
嘛,这题其实跟昨天的 567 题是大同小异,这区别小的我直接把那题的代码改一下就过了,所以 567 题算中等难度,这题也能算中等难度吧。
其实主要的区别就是检查到第一个异位词之后从直接返回变成向数组里面添加下标,然后最后返回数组,仅此而已,所以可以直接看代码,应该没什么特别困难的地方。
完整代码:
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
int cnt[26] = {0};
for(auto c : p) cnt[c - 'a']++;
vector<int> ans;
int left = 0, right = 0;
while(right < s.size()) {
cnt[s[right] - 'a']--;
while(cnt[s[right] - 'a'] < 0) {
cnt[s[left] - 'a']++;
left++;
}
if(right - left + 1 == p.size()) ans.push_back(left);
right++;
}
return ans;
}
};
/www/wwwroot/www.yuisblog.com/usr/themes/dinner/post.php on line 86
" itemprop="url" class="inline-block mb-3 shadow mx-0.5 px-2 py-1 text-gray-900 dark:text-gray-100 bg-white dark:bg-gray-700 rounded-lg">#C++编程 /www/wwwroot/www.yuisblog.com/usr/themes/dinner/post.php on line 86
" itemprop="url" class="inline-block mb-3 shadow mx-0.5 px-2 py-1 text-gray-900 dark:text-gray-100 bg-white dark:bg-gray-700 rounded-lg">#力扣打卡
" itemprop="url" class="inline-block mb-3 shadow mx-0.5 px-2 py-1 text-gray-900 dark:text-gray-100 bg-white dark:bg-gray-700 rounded-lg">#C++编程 /www/wwwroot/www.yuisblog.com/usr/themes/dinner/post.php on line 86
" itemprop="url" class="inline-block mb-3 shadow mx-0.5 px-2 py-1 text-gray-900 dark:text-gray-100 bg-white dark:bg-gray-700 rounded-lg">#力扣打卡
版权属于:我是苏云曦吖
本文链接:https://www.yuisblog.com/archives/229/
本站未注明转载的文章均为原创,并采用
CC BY-NC-SA 4.0 授权协议,转载请注明来源,谢谢!