leetcode hot 06
1.leetcode hot 012.leetcode hot 023.leetcode hot 044.leetcode hot 035.leetcode hot 05
6.leetcode hot 06
7.leetcode hot 078.leetcode hot 089.leetcode hot 0910.leetcode hot 1011.leetcode hot 1212.leetcode hot 1313.leetcode hot 1414.leetcode hot 1515.leetcode hot 1116.leetcode hot 1617.leetcode hot 1718.leetcode hot 1819.leetcode hot 1920.leetcode hot 2021.leetcode hot 2122.leetcode hot 2223.leetcode hot 2324.leetcode hot 2425.leetcode hot 25解题思路:需要使用hashmap,定义是(<String,List()>)遍历数组内每一个字符串,转为字符数组后用Arrays.sort()排序,如果hashmap.containsKey没有对应的key,就将排序后的字符数组转回字符串(new String(arr)),以(s_new,new List())的形式put进入hashmap,然后将原来的字符串add如字符列表中,然后下一轮遍历。直到最后所有都放进去再用list.addAll(hashmap.values())的方式返回list。
import java.util.Arrays;
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> list = new ArrayList<>();
Map<String,List<String>> hashmap = new HashMap<>();
for(String str:strs){
char[] arr = str.toCharArray();
Arrays.sort(arr);
String s = new String(arr);
if(!hashmap.containsKey(s)){
hashmap.put(s,new ArrayList<String>());
}
hashmap.get(s).add(str);
}
list.addAll(hashmap.values());
return list;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~