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"]
]
public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<String, List<String>>();
for(String str : strs){
// 将单词按字母排序
char[] carr = str.toCharArray();
Arrays.sort(carr);
String key = new String(carr);
List<String> list = map.get(key);
if(list == null){
list = new ArrayList<String>();
}
list.add(str);
map.put(key, list);
}
List<List<String>> res = new ArrayList<>();
// 将列表按单词排序
for(String key : map.keySet()){
List<String> curr = map.get(key);
Collections.sort(curr);
res.add(curr);
}
return res;
}
}
Group Anagrams的更多相关文章
- LeetCode - 49. Group Anagrams
49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...
- 【Leetcode】【Medium】Group Anagrams
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- 49. Group Anagrams
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- LeetCode49 Group Anagrams
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- leetcode@ [49] Group Anagrams (Hashtable)
https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...
- 【LeetCode】49. Group Anagrams
题目: Given an array of strings, group anagrams together. For example, given: ["eat", " ...
- 【一天一道LeetCode】#49. Group Anagrams
一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...
- Group Anagrams 群组错位词
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- [Swift]LeetCode49. 字母异位词分组 | Group Anagrams
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
随机推荐
- Create new tool for CSV
CsvFileStream.cs public class CsvFileStream { TextReader stream; bool EOS = false; bool EOL = false; ...
- guid正则表达
a-fA-F0-9 加上下划线 _ 可以用 \w 来代替. ^\w{8}-(\w{4}-){3}\w{12}$ 如果不可以用下划线, 0-9 用 \d 代替 a-fA-F 就用其中一个 a-f,然后匹 ...
- jQuery,title、仿title功能整理
如图:仿 title="查看" note="查看",note 可换成其他 样式: /*重写,标签title层*/#titleRewrite {position: ...
- EF: Raw SQL Queries
Raw SQL Queries Entity Framework allows you to query using LINQ with your entity classes. However, t ...
- YII2 自定义日志路径
YII 提供的日志写入方法: 1.Yii::getLogger()->log($message, $level, $category = 'application') 2.Yii::trace( ...
- linux如何隐藏和显示所有窗口?
centos7 基本上就跟fedora23是一样的了, 也许它们使用的内和是一样的, fedora23使用的是4.2.3, 所以使用fedora对使用centos和redhat是有优势和好处福利的. ...
- 机器学习的5种语言(选自info world)
摘要:机器学习目前炙手可热,本文搜集了Java.Python以及go等编程语言中常见且实用的开源机器学习工具,对机器学习感兴趣的开发者或者准备和机器学习打交道的数据科学家们不能错过了 [编者按] 机器 ...
- NOIP2010 引水入城 题解
http://www.rqnoj.cn/problem/601 今天发现最小区间覆盖竟然是贪心,不用DP!于是我又找到这题出来撸了一发. 要找到最上面每个城市分别能覆盖最下面哪些城市,如果最下面有城市 ...
- 使用DOS比较两个txt文件的差异
将两个文件放入到同一个文件夹下 DOS下提供了FC命令 点击开始->运行->输入cmd,进入DOS下,进入指定目录,输入FC a.txt b.txt进行比较,下面会显示出之间的差异
- jQuery源码-class操作
写在前面 本文写作基于jQuery 1.9.1版本,源码分析系列目录:http://www.cnblogs.com/chyingp/archive/2013/06/03/jquery-souce-co ...