思路 : 字母异位词是字母排序不同,但字母总量相同的字符串,可以用一个排序后的String充当key,一个List收集对应该String的全部异位词。
主要API用法:
toCharArray : String转char类型数组
map.values() 取出Map的全部value值作为一个列表。
如果有疑问和更好的见解欢迎交流
class Solution {
public List<List> groupAnagrams(String[] strs) {
//toCharArray
//Arrays.sort()
List<List> res = new ArrayList();
Map<String,List> m = new HashMap();
for(int i = 0;i<strs.length;++i){
char[] cArray = strs[i].toCharArray();
Arrays.sort(cArray);
String tempString = new String(cArray);
if(m.containsKey(tempString)){
List temp = m.get(tempString);
temp.add(strs[i]);
m.put(tempString,temp);
}
else{
List temp = new ArrayList();
temp.add(strs[i]);
m.put(tempString, temp);
}
}
return new ArrayList<List>(m.values());
}
}