Given an array of strings, group anagrams together.
Example:Input: ["eat", "tea", "tan", "ate", "nat", "bat"],Output:[ ["ate","eat","tea"], ["nat","tan"], ["bat"]]
Note:
All inputs will be in lowercase.The order of your output does not matter.难度:medium
题目:给定一个字符串数组,将 颠倒字母而成的单词 组合在一起。
注意:所有输入都为小写字母思路:都按字母序排序。
Runtime: 14 ms, faster than 85.30% of Java online submissions for Group Anagrams.
Memory Usage: 34.1 MB, less than 16.58% of Java online submissions for Group Anagrams.class Solution { public List
> groupAnagrams(String[] strs) { Map > msl = new HashMap<>(); for (int i = 0; i < strs.length; i++) { char[] sc = strs[i].toCharArray(); Arrays.sort(sc); String ns = new String(sc); msl.putIfAbsent(ns, new ArrayList ()); msl.get(ns).add(strs[i]); } return new ArrayList(msl.values()); }}