题目导航题目编号: 49题目名称: 字母异位词分组题目链接:LeetCode 49题https://leetcode.com/problems/group-anagrams/难度: 中等标签: 哈希表、字符串、排序题目描述给定一个字符串数组strs将其中所有字母异位词组合在一起。字母异位词指字母相同但排列不同的字符串。示例:输入:strs [eat,tea,tan,ate,nat,bat]输出:[[bat],[nat,tan],[ate,eat,tea]]说明:所有输入均为小写字母。不考虑答案输出的顺序。题解我使用的是排序哈希表的做法具体思路以注释的形式放在代码中非常好理解。class Solution { public ListListString groupAnagrams(String[] strs) { //字母异位词字母内容相同但是顺序不同 //哈希表记录答案 //新建哈希表 MapString,ListString map new HashMap(); //遍历字符串数组 for(String str:strs){ //取每个字符串进行排序和与哈希表比较 char[] s str.toCharArray(); Arrays.sort(s); //排序后与哈希表比对 if(map.containsKey(new String(s))){ //若是原先就有排序后的字符串那么之间取来值将原字符串添加到哈希表中 map.get(new String(s)).add(str); }else{ //若是map中没有对应的键和值那么新建一个list将其放入 ListString temp new ArrayList(); temp.add(str); map.put(new String(s),temp);//键是排序后的字符串值是原来的字符串组成的列表 } } return new ArrayList(map.values());//map的列表值的列表 } }