您的位置:首页 > 其它

Anagrams

2014-03-01 11:23 218 查看
Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.

便利输入字符串,用unordered_map(类似hash_map)保存访问信息,最后便利map把anagram按组打印出来。

-------------------------------

        vector<string> ret;

        unordered_map<string, list<int>> vmap;

        for (int i = 0; i < strs.size(); i++) {

            string curs = strs[i];

            sort(curs.begin(), curs.end());

            vmap[curs].push_back(i);

        }

        

        for (auto it = vmap.begin(); it != vmap.end(); it++) {

            if (it->second.size() < 2) continue;

            for (auto lit = it->second.begin(); lit != it->second.end(); lit++) {

                ret.push_back(strs[*lit]);

            }

        }

        

        return ret;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: