给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
示例:
输入: ["eat", "tea", "tan", "ate", "nat", "bat"],
输出:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
说明:
所有输入均为小写字母。
不考虑答案输出的顺序。
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String,List<String>> map= new HashMap<>();
List<List<String>> res= new ArrayList<>();
for(int i=0;i<strs.length;i++){
char[] c=strs[i].toCharArray();
Arrays.sort(c);
String key = String.valueOf(c);
if(!map.containsKey(key))
{
map.put(key,new ArrayList<String>());
}
map.get(key).add(strs[i]);
}
for(List<String> l:map.values())
{
res.add(l);
}
return res;
}
}
最后
以上就是风趣电源最近收集整理的关于Leetcode(Java)-49. 字母异位词分组的全部内容,更多相关Leetcode(Java)-49.内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复