字符串 Substring with Concatenation of All Words
来源:互联网 发布:灯光设计软件 编辑:程序博客网 时间:2024/05/25 08:12
思想:给定s,找符合条件的串s',该s'包括给定words中的每一个word且仅包含1次,不包含其他字符,返回若干个s'的起始位置。
学习unordered_map:
unordered_map是一种无序的、键值对应的哈希容器。
允许通过key快速索引value,不允许多个value对应到同一个key。
构造函数:unordered_map(unordered_map t)
erase函数:
iterator erase ( const_iterator position );
size_type erase ( const key_type& k );
iterator erase ( const_iterator first, const_iterator last );
find函数:
Get iterator to element
Searches the container for an element with k as key and returns an iterator to it if found, otherwise it returns an iterator to unordered_map::end (the element past the end of the container).迭代器的使用:
Iterators of a unordered_map container point to elements of this value_type. Thus, for an iterator called it that points to an element of a map, its key and mapped value can be accessed respectively with:
1234
unordered_map<Key,T>::iterator it;(*it).first; // the key value (of type Key)(*it).second; // the mapped value (of type T)(*it); // the "element value" (of type pair<const Key,T>)
Naturally, any other direct access operator, such as -> or [] can be used, for example:
12
it->first; // same as (*it).first (the key value)it->second; // same as (*it).second (the mapped value)
class Solution {public: vector<int> findSubstring(string s, vector<string>& words) { size_t wordLength = words.front().length(); size_t catLength = words.size()*wordLength; vector<int> res; if(s.length() < catLength) return res; unordered_map<string, int> wordCount; for(auto word : words) { wordCount[word]++; } for(auto i=s.begin(); i <= prev(s.end(), catLength); i++) { unordered_map<string, int> unused(wordCount); for(auto j = i; j != next(i,catLength); j+=wordLength) { auto pos = unused.find(string(j, next(j,wordLength))); if(pos == unused.end() || pos->second == 0) break; pos->second--; if(pos->second == 0) unused.erase(pos); } if(unused.size() == 0) res.push_back(distance(s.begin(), i)); } return res; }};
0 0
- 字符串 Substring with Concatenation of All Words
- LeetCode: Substring with Concatenation of All Words
- LeetCode : Substring with Concatenation of All Words
- LeetCode29: Substring with Concatenation of All Words
- [Leetcode] Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- [LeetCode]Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- LeetCode-Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- 26 - Substring with Concatenation of All Words
- LeetCode:Substring with Concatenation of All Words
- Leetcode: Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- Substring with Concatenation of All Words
- 关于react native运用的简单总结
- mysql数据备份导入导出详解
- 编程之美8:链表常见面试笔试题集合
- NuGet来管理你的包——成绩登统系统
- 组合测试法中的全对偶测试法
- 字符串 Substring with Concatenation of All Words
- 关于类似“Can't bind to local 8602 for debugger”错误的解决方案
- 《从非结构化数据到大数据》有感
- eclipse调试多线程
- kali可用的更新源
- java web中的内容
- 二分匹配_HDU_1068
- Python音乐播放器
- Lua动态加载模块