代码:
这题好像写麻烦了
- class Solution {
- public List
> groupAnagrams(String[] strs) {
- int n = strs.length;
- List
> res = new ArrayList<>();
- UnionFind uf = new UnionFind(n);
- for(int i=0;i
- for(int j=i+1;j
- if(strs[i].length()!=strs[j].length())continue;
- char[] arrayi = strs[i].toCharArray();
- char[] arrayj = strs[j].toCharArray();
- Arrays.sort(arrayi);
- Arrays.sort(arrayj);
- boolean f = true;
- for(int k=0;k
- if(arrayi[k]!=arrayj[k]){
- f = false;
- break;
- }
- }
- if(f==true){
- uf.union(i,j);
- }
- }
- }
- for(int i=0;i
- List
list = new ArrayList<>(); - for(int j=0;j
- if(uf.find(j)==i){
- list.add(strs[j]);
- }
- }
- if(list.size()>0){
- res.add(list);
- }
- }
- return res;
- }
-
- }
- class UnionFind{
- int[] parent;
- public UnionFind(int n){
- parent = new int[n];
- for(int i=0;i
- }
- public int find(int idx){
- if(parent[idx]!=idx){
- parent[idx] = find(parent[idx]);
- }
- return parent[idx];
- }
- public void union(int idx1,int idx2){
- parent[find(idx2)] = find(idx1);
- }
- }
-
相关阅读:
纯c语言 算法bin文件转换为数组
扎实打牢数据结构算法根基,从此不怕算法面试系列之007 week01 02-07 简单的复杂度分析
混淆矩阵和相应参数详解
【附源码】计算机毕业设计JAVA技术交流网站
在MySQL中使用VARCHAR字段进行日期筛选
【仁川出差】记2022.10仁川出差
[附源码]Python计算机毕业设计Django的物品交换平台
node.js基础学习
SystemV共享内存
cuda文档链接
-
原文地址:https://blog.csdn.net/stacey777/article/details/133855974
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU