【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 * ...
随机推荐
- C#之自定义特性
在前面介绍的代码中有使用特性,这些特性都是Microsoft定义好的,作为.NET Framework类库的一部分,许多特性都得到了C#编译器的支持. .NET Frmework也允许定义自己的特性. ...
- JS 中new一个对象发生了什么事
今天看到一个360的前端面试题: function A(){}function B(a){ this.a=a;}function C(a){ if(a){ this.a=a; }}A.p ...
- Spring Cloud 注册中心Eureka
一.简介 最近在看Spring Cloud微服务,接下来的时间和大家一起分享我所看到的,公司现在用的是dubbo ,之后有时间也去了解了解dubbo的源码.与dubbo相比较,Spring Cloud ...
- JSP----获取表单参数
在页面中可大量使用 request 对象来获取表单域的值,获取表单域的值有如下两个 方法. • String getParamete(String para mN ame): 获取表单域的值. • S ...
- How To Configure VMware fencing using fence_vmware_soap in RHEL High Availability Add On(RHEL Pacemaker中配置STONITH)
本文主要简单介绍一下如何在RHEL 7 Pacemaker中配置一个fence_vmware_soap类型的STONITH设备(仅供测试学习). STONITH是Shoot-The-Other-Nod ...
- 第三章(附)mysql表类型MyISAM和InnoDB区别(决定了是否支持事务)
mysql表类型MyISAM和InnoDB区别 MyISAM:这个是默认类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的顺序访问 ...
- cmd批处理延迟代码 结束进程
choice /t 5 /d y /n >nul taskkill /im chrome.exe /f pause
- Mac下配置Nginx负载均衡
1.首先在Mac下安装Nginx(可参考我的另一篇随笔http://www.cnblogs.com/malcolmfeng/p/6896703.html). 2.安装Tomcat,下载后,解压,bin ...
- My "Top 5 R Functions"(转)
In preparation for a R Workgroup meeting, I started thinking about what would be my "Top 5 R Fu ...
- [深圳/广州]微软SQL技术沙龙分享会(MVP)
[深圳/广州] 新一期俱乐部活动报名开始,这次是广深地区SQL Server 技术沙龙分享会(MVP),SQL Server作为一个数据平台,不管是SQL Server 2017 on Linux 还 ...