leetcode 49 字母异位词分组

简介

sort + map 很简洁

code

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> m = new HashMap<>();
        for(String str : strs) {
            char [] a = str.toCharArray();
            Arrays.sort(a);
            String key = new String(a);
            List<String> l = m.getOrDefault(key, new LinkedList<String>());

            l.add(str);
            m.put(key, l);
        }
        return new LinkedList<List<String>>(m.values());
    }
}
Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
原文地址:https://www.cnblogs.com/eat-too-much/p/14882491.html