【LeetCode】49. Group Anagrams
题目:
Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
, Return:
[
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]
Note:
- For the return value, each inner list's elements must follow the lexicographic order.
- All inputs will be in lower-case.
提示:
这道题要把所有字母组成相同的单词归为一类。因此我们可以把每个字母都进行排序,然后利用一个hash_map保存。即以排序后的结果作为键,map的值可以是一个set,把排序前的结果插入到set当中。由于set的底层实现利用了平衡二叉搜索树,所以插入以后的元素是已经排好序的。这样归类就完成了。
代码:
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
if (strs.empty()) {
return res;
}
unordered_map<string, multiset<string>> um;
for (string str : strs) {
string tmp = str;
sort(tmp.begin(), tmp.end());
um[tmp].insert(str);
}
for (auto m : um) {
vector<string> sol(m.second.begin(), m.second.end());
res.push_back(sol);
}
return res;
}
};
实际上,由于对单词排序时,题目已经限定了单词只可能是26个小写字母组成的,所以我们可以使用计数排序进一步加快算法的速度(排序部分速度从O(nlogn)变为O(n)),代码如下:
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
if (strs.empty()) {
return res;
}
unordered_map<string, multiset<string>> um;
for (string str : strs) {
string tmp = strSort(str);
um[tmp].insert(str);
}
for (auto m : um) {
vector<string> sol(m.second.begin(), m.second.end());
res.push_back(sol);
}
return res;
} string strSort(string s) {
vector<int> count(, );
for (int i = ; i < s.length(); ++i) {
++count[s[i] - 'a'];
}
string res = "";
for (int i = ; i < ; ++i) {
while (count[i]--) {
res += ('a' + i);
}
}
return res;
}
};
【LeetCode】49. Group Anagrams的更多相关文章
- 【LeetCode】49. Group Anagrams 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+hash 日期 题目地址:https://le ...
- 【一天一道LeetCode】#49. Group Anagrams
一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...
- 【leetcode】Find All Anagrams in a String
[leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...
- LeetCode OJ 49. Group Anagrams
题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...
- 【LeetCode】49. Anagrams (2 solutions)
Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
- LeetCode:49. Group Anagrams(Medium)
1. 原题链接 https://leetcode.com/problems/group-anagrams/description/ 2. 题目要求 给定一个字符串数组,将数组中包含相同字母的元素放在同 ...
- 【leetcode】1282. Group the People Given the Group Size They Belong To
题目如下: There are n people whose IDs go from 0 to n - 1 and each person belongs exactly to one group. ...
- 【LeetCode】49. 字母异位词分组
49. 字母异位词分组 知识点:字符串:哈希表 题目描述 给你一个字符串数组,请你将 字母异位词 组合在一起.可以按任意顺序返回结果列表. 字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源 ...
- 【LEETCODE】49、数组分类,简单级别,题目:566,1089
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
随机推荐
- 不依赖浏览器控制台的JavaScript断点调试方法
随着浏览器的逐渐强大,绝大多数情况下的代码调试都是可以通过浏览器自带的一些调试工具进行解决.然而对于一些特殊情况仍然无法享受到浏览器的强大 调试能力,比如QQ客户端内嵌web的调试(虽然说QQ目前已经 ...
- 如何使用.bas文件
1. 确保你安装的是word 2010,打开word文档,,按ALT + F11打开VBE编辑器. 2.点击Normal,右键,在弹出的对话框中选择导入文件. 2. 选择需要使用的脚本的位置,然后点击 ...
- BeanInstantiationException: Failed to instantiate [java.time.LocalDateTime]
错误提示: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationExce ...
- ng-option
select 是 AngularJS 预设的一组directive.下面是其官网api doc给出的用法:AngularJS:select 大意是,select中的ngOption可以采用和ngRep ...
- trap-接收信号_采取行动
trap命令用于指定在接收到信号后将要采取的动作,常见的用途是在脚本程序被中断时完成清理工作. kill和trap等都可以看到信号编号及其关联的名称. "信号"是指那些被异步发送到 ...
- 华为路由器AR1220F-S的端口映射NAT配置(拨号光纤上网)
telnet 登录 或者ssh登录路由器 //进入系统试图界面 sys-view //第一步. 添加acl规则, 允许内网本身访问对外的公网ip. 否则,只能外部人员访问你的公网ip [Huawei] ...
- hexo从零开始到搭建完整
前言 其实平时自己写的文章并不多,偶尔看到一些东西会做点笔记,但是每次写的东西都会到处放,不好找,所以才想着自己搭建一个人博客网站,现在大家用hexo比较多,也比较方便,并且能使用的主题也很多,所以小 ...
- Telegram学习解析系列(三) : Build Telegram报错分析总结
正好通过这次 Telegram 的运行,很想把常见的项目运行的错误好好的总结一下,在前面的博客中,又星星散散的总结过错误和一些警告的消除方法,这次把错误处理一下,还有Telegram项目中有999+的 ...
- nginx+tomcat+session共享(转)
1 起因 最近对新开发的web系统进行了压力测试,发现tomcat默认配置下压到600人的并发登录首页响应速度就有比较严重的影响,一轮出现2000多个的 500和502错误.我把登录的时间统计做了 ...
- 源码阅读—Iterator接口和LIstIterator接口
在继续看ArrayList源码之前,先了解Iterator接口和ListIterator接口,下篇文章详细讲解ArrayList是如何实现它们的. 我们知道,接口只是一种规范,当继承接口并实现其中的方 ...