在局域网建设网站,透明管理系统网站模板,深圳市九号公告最新消息,手机开网店题目解析
30. 串联所有单词的子串 本题的意思就是在目标串s中寻找能够找到的words字符串的全排列#xff0c;返回起始位置 算法讲解 我们可以将这道题转化为寻找目标串的words字母的异位词#xff0c;按照上一次讲解的【滑动窗口】Leetcode 找到字符串中所有字母异位词我们…题目解析
30. 串联所有单词的子串 本题的意思就是在目标串s中寻找能够找到的words字符串的全排列返回起始位置 算法讲解 我们可以将这道题转化为寻找目标串的words字母的异位词按照上一次讲解的【滑动窗口】Leetcode 找到字符串中所有字母异位词我们还是使用同样的做法哈希表 滑动窗口 但是这道题有以下注意事项滑动窗口的移动次数 每一次left和right一开始都指向同一个位置当滑动窗口移动到字符串s结束的时候需要将left1开始继续滑动下一次的循环 class Solution {
public:
vectorint findSubstring(string s, vectorstring words) {unordered_mapstring, int Hash_words;vectorintret;int left 0;int right 0;//将words放进Hashfor (auto str : words){Hash_words[str];}int count 0;int cnt 0;//窗口一次移动完成之后再从一开始的下一个位置反复while (cnt words[0].size()){//这是一次完整的移动unordered_mapstring, int hash2; // 维护窗⼝内单词的频次for (int left cnt, right cnt, count 0; right words[0].size() s.size(); right words[0].size()){// 进窗⼝ 维护 countstring temp s.substr(right, words[0].size());hash2[temp];//这里hash2[temp] Hash_words[temp]时还需要再count因为有可能遇到s中连续相同的串我要确保当前位置的串和后面的串能利用上if (Hash_words.count(temp) hash2[temp] Hash_words[temp]){count;}// 判断if (right - left 1 (words.size() * words[0].size())){// 出窗⼝ 维护 countstring out s.substr(left, words[0].size());if (Hash_words.count(out) hash2[out] Hash_words[out]) count--;hash2[out]--;left words[0].size();}// 更新结果if (count words.size()){ret.push_back(left);}}cnt;}return ret;
}
};