leetcode - Anagrams

来源:互联网 发布:n的阶乘算法 编辑:程序博客网 时间:2024/05/20 04:11

Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.

class Solution {public:    std::vector<std::string> anagrams(std::vector<std::string> &strs) {        std::vector<std::string> res;std::map<std::string, int> ans;for (int i = 0; i < strs.size(); i++){std::string s = strs[i];std::sort(s.begin(),s.end());if(ans.find(s) != ans.end()){if(ans[s] >= 0){res.push_back(strs[ans[s]]);ans[s] = -1;}res.push_back(strs[i]);}else{ans.insert(std::make_pair(s,i));}}return res;    }};


0 0
原创粉丝点击