2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_49.java 1017 Bytes
一键复制 编辑 原始数据 按行查看 历史
CG国斌 提交于 2020-09-21 10:42 . optimize solution of 3 & 49
package com.hit.basmath.learn.hash_table;
import java.util.*;
/**
* 49. Group Anagrams
* <p>
* Given an array of strings, group anagrams together.
* <p>
* Example:
* <p>
* Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
* <p>
* Output:
* <p>
* [
* ["ate","eat","tea"],
* ["nat","tan"],
* ["bat"]
* ]
* <p>
* Note:
* <p>
* All inputs will be in lowercase.
* The order of your output does not matter.
*/
public class _49 {
public List<List<String>> groupAnagrams(String[] strs) {
if (strs == null || strs.length == 0) return new ArrayList<>();
Map<String, List<String>> map = new HashMap<String, List<String>>();
for (String s : strs) {
char[] ca = s.toCharArray();
Arrays.sort(ca);
String keyStr = String.valueOf(ca);
if (!map.containsKey(keyStr)) {
map.put(keyStr, new ArrayList<>());
}
map.get(keyStr).add(s);
}
return new ArrayList<>(map.values());
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助