【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 * ...
随机推荐
- 北京赛车PK10 幸运飞艇 重庆时时彩 PC蛋蛋 快乐8 福彩3D 十分彩
QQ:1395239152 2017-3.14最新修复完整运营版时时彩源码PC+手机版本功能齐全 重庆时时彩是一种经中国国家财政部批准,重庆市福利彩票发行中心承销的福彩快开彩票,2元1注,分为&quo ...
- arm处理器
arm处理器 arm处理器相关 1.体系架构定义了指令集(ISA)和基于这一体系结构下处理器的编程模型. arm卖的是架构或者已经设计好的公版ip核 卖给苹果高通的是架构,需要苹果高通通过架构设计自己 ...
- SQLite 之 C#版 System.Data.SQLite 使用
简介 SQLite简介 SQLite,是一款轻型的关系型数据库.它的设计目标是嵌入式. 它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 C++.C ...
- JS执行事件
先贴出几个名词: 同步任务: 在主线程上排队执行的任务,只有前一个任务执行完毕,才能执行后一个任务 异步任务: 不进入主线程,而进入"任务队列"的任务,只有任务队列通知主线程, ...
- Day1-while and for/break and continue
一.while and for 需求:猜年龄,输错三次退出,猜对退出 解决1: age_of_oldboy = 56count = 0while True: if count == 3: print( ...
- EntityFramework6.X之DataAnnotations
DataAnnotations 在web开发中不仅在客户端需要执行验证逻辑,会对会对用户向表单中输入的数据给出一个即时反馈:且在服务器端也需验证逻辑,因为来自网络的信息都是不能信任的.在MVC中通常是 ...
- 【WPF MaterialDesign 示例开源项目】 Work Time Manager
转岗写了将近一年的 PHP 最近因为 工作太多太杂, 在汇报工作的时候经常会忘记自己做了些什么,本来想只是使用excel来记录,但是发现了excel的很多局限性,光是无法共享就郁闷死了,习惯了下班不带 ...
- struts2.1.6教程十一、注解配置
在此先略去注解配置的实例,具体可以参看官方提供的文档.其实在熟悉struts及相关的一些内容后,再来看文档是比较容易理解得.只是要注意使用注解Annotition时: (1)要多导入一个jar包:st ...
- springboot 1.5.2 集成kafka 简单例子
添加依赖 compile("org.springframework.kafka:spring-kafka:1.1.2.RELEASE") 添加application.propert ...
- nested exception is java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 14 to TIMESTAMP.
无法将"0000-00-00 00:00:00"转换为TIMESTAMP 2017-05-08 00:56:59 [ERROR] - cn.kee.core.dao.impl.Ge ...