代码:
这题好像写麻烦了
- 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);
- }
- }
-
相关阅读:
【节能学院】智能操控装置在高压开关柜的应用
KVM Forum 2022应该关注的话题
CSS3 选择器、Sass选择器、在小程序中使用的选择器(wxss为例)
buuctf web [极客大挑战 2019]LoveSQL
pikachu ssrf
字符串的匹配——KMP算法的学习
电脑启动过程(超详细过程)
Docker 学习路线 13:部署容器
七通道NPN 达林顿管GC2003,专为符合标准 TTL 而制造,最高工作电压 50V,耐压 80V
二维卷积输出特征图的计算公式
-
原文地址:https://blog.csdn.net/stacey777/article/details/133855974