[LeetCode]Anagrams
来源:互联网 发布:淘宝店铺手机号收费吗 编辑:程序博客网 时间:2024/04/29 06:17
class Solution {//anagrams represent two string consists the same characters//should pay attention to the meaning of anagramspublic:vector<string> anagrams(vector<string> &strs) {// Start typing your C/C++ solution below// DO NOT write int main() functionmap<string, vector<string>> dict;for (int i = 0; i < strs.size(); ++i){string word = strs[i];sort(word.begin(), word.end());dict[word].push_back(strs[i]);}//check if they are anagramsmap<string, vector<string>>::iterator it;vector<string> ans;for (it = dict.begin(); it != dict.end(); it++){if (it->second.size() > 1) //then they forms an anagrams{for(int i = 0; i < it->second.size(); ++i)ans.push_back(it->second[i]);}}return ans;}};
second time
class Solution {public: vector<string> anagrams(vector<string> &strs) { // Start typing your C/C++ solution below // DO NOT write int main() function map<string, int> baseWord; for(int i = 0; i < strs.size(); ++i) { string tmpStr = strs[i]; sort(tmpStr.begin(), tmpStr.end()); if(baseWord.find(tmpStr) == baseWord.end()) baseWord[tmpStr] = 1; else baseWord[tmpStr]++; } vector<string> ans; for(int i = 0; i < strs.size(); ++i) { string tmpStr = strs[i]; sort(tmpStr.begin(), tmpStr.end());//this can be optimized if(baseWord.find(tmpStr) != baseWord.end() && baseWord[tmpStr] > 1) ans.push_back(strs[i]); } return ans; }};
- LeetCode: Anagrams
- LeetCode: Anagrams
- LeetCode Anagrams
- Leetcode: Anagrams
- [Leetcode] Anagrams
- LeetCode : Anagrams
- [leetcode] anagrams
- LeetCode Anagrams
- [LeetCode] Anagrams
- [LeetCode]Anagrams
- [leetcode]Anagrams
- leetcode anagrams
- [leetcode] Anagrams
- LeetCode-Anagrams
- LeetCode - Anagrams
- Leetcode: Anagrams
- 【Leetcode】Anagrams
- LeetCode Anagrams
- 软件测试用例的认识误区
- [LeetCode]Add Binary
- 类加载器
- [LeetCode]Add Two Numbers
- hibernate中对象状态转换图
- [LeetCode]Anagrams
- I2C通讯 面面谈(一)
- Unigui的UniForm增加ToolTip
- linux用户层串口操作
- Android 互斥button 解决方法
- NodeJS v0.10.8升级安装
- [LeetCode]Balanced Binary Tree
- 开源引路蜂地图开发包
- C++ 下基于迭代器适配器和范型算法的排序