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;
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;
相关文章推荐
- Linux—文件系统
- php无限级分类
- Android动画效果translate、scale、alpha、rotate详解
- chrome 如何开启网页另存为.mhtml 功能
- 一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现
- windows操作系统的密码复杂性要求
- GIMP图片描边
- C-最长回文子串(2)
- python第三站:运动员数据筛选
- android ImageView 设置背景选择器selector
- 【stm32+uC/OS-II】ucosii移植简单详细步骤
- MMORPG大型游戏设计与开发(规范)
- 装腔,你不能不学
- Java环境配置速记
- hostapd.conf配置文档
- Java学习笔记List集合
- 装腔,你不能不学
- module_init 详解
- 斗破苍穹RPG
- 百度空间:飘动的天空 有些看到的好文章收录在此处就不拷过来了 有兴趣的朋友可以看下