Union Find - 20181102 - 20181105
Union Find:
589. Connecting Graph
public class ConnectingGraph {
//父节点数组
private int[] father = null; /*
* @param n: An integer
*/
public ConnectingGraph(int n) {
// do intialization if necessary
father = new int[n + 1];
for (int i = 1; i <= n; i++) {
father[i] = i;
}
} /*
* @param a: An integer
* @param b: An integer
* @return: nothing
*/
public void connect(int a, int b) {
// write your code here
int root_a = find(a);
int root_b = find(b);
if (root_a == root_b) {
return;
}
father[root_a] = root_b;
} /*
* @param a: An integer
* @param b: An integer
* @return: A boolean
*/
public boolean query(int a, int b) {
// write your code here
return find(a) == find(b);
} //union find 主要逻辑,实现找到每个节点的父节点,同时把寻找父节点的路径子节点都直接链接到root节点下
//为了减少记录path的空间分配,可以每次将路径中子节点指向父节点的下一个节点,同样可以实现路径压缩
public int find(int a) {
if (father[a] == a) {
return a;
}
int i = a;
while (father[i] != i) {
int b = father[i];
father[i] = father[b];
i = b;
}
return i;
}
}
590. Connecting Graph II
public class ConnectingGraph2 {
private int[] father = null;
private int[] size = null; /*
* @param n: An integer
*/
public ConnectingGraph2(int n) {
// do intialization if necessary
father = new int[n + 1];
size = new int[n+1];
for (int i = 1; i <= n; i++) {
father[i] = i;
size[i] = 1;
}
} /*
* @param a: An integer
* @param b: An integer
* @return: nothing
*/
public void connect(int a, int b) {
// write your code here
int root_a = find(a);
int root_b = find(b);
if (root_a != root_b) {
father[root_a] = root_b;
size[root_b] += size[root_a];
}
} /*
* @param a: An integer
* @return: An integer
*/
public int query(int a) {
// write your code here
int root_a = find(a);
return size[root_a];
} public int find(int a) {
if (father[a] == a) {
return a;
} Set<Integer> path = new HashSet<>();
int i = a;
while (father[i] != i) {
path.add(i);
i = father[i];
}
for (int num : path) {
father[num] = i;
} return i; }
}
591. Connecting Graph III
public class ConnectingGraph3 {
/**
* @param a: An integer
* @param b: An integer
* @return: nothing
*/
private int[] father = null;
private int count = 0; public ConnectingGraph3(int n) {
father = new int[n + 1];
for (int i = 1; i <= n; i++) {
father[i] = i;
}
count = n;
} public void connect(int a, int b) {
// write your code here
int root_a = find(a);
int root_b = find(b);
if (root_a != root_b) {
father[root_a] = root_b;
count--;
}
} /**
* @return: An integer
*/
public int query() {
// write your code here
return count;
} public int find(int a) {
if (father[a] == a) {
return a;
}
int i = a;
while (father[i] != i) {
int b = father[i];
father[i] = father[b];
i = b;
}
return i;
}
}
1070. Accounts Merge(二刷复习)
public class Solution {
/**
* @param accounts: List[List[str]]
* @return: return a List[List[str]]
*/
//integer对应accounts的index
Map<Integer, Integer> father = new HashMap<>(); public List<List<String>> accountsMerge(List<List<String>> accounts) {
// write your code here
List<List<String>> res = new ArrayList<>();
if (accounts == null || accounts.size() == 0) {
return res;
}
Map<String, List<Integer>> emailToIds = buildEmailToIds(accounts);
for (Map.Entry<String, List<Integer>> entry : emailToIds.entrySet()) {
List<Integer> ids = entry.getValue();
if (ids != null && ids.size() != 0) {
for (int i = 0; i < ids.size(); i++) {
union(ids.get(i), ids.get(0));
}
}
} Map<Integer, Set<String>> idToEmails = buildIdToEmails(accounts);
for (Map.Entry<Integer, Set<String>> entry : idToEmails.entrySet()) {
List<String> mergeList = new ArrayList<>();
List<String> accountList = new ArrayList<>(entry.getValue());
Collections.sort(accountList);
String user = accounts.get(entry.getKey()).get(0);
mergeList.add(user);
mergeList.addAll(accountList);
res.add(mergeList);
} return res;
} public Map<String, List<Integer>> buildEmailToIds(List<List<String>> accounts) {
Map<String, List<Integer>> res = new HashMap<>();
for (int i = 0; i < accounts.size(); i++) {
if (accounts.get(i) == null || accounts.get(i).size() <= 1) {
continue;
}
father.put(i, i);
List<String> emails = accounts.get(i);
for (int j = 1; j < emails.size(); j++) {
List<Integer> ids = res.get(emails.get(j));
if (ids == null) {
ids = new ArrayList<>();
}
ids.add(i);
res.put(emails.get(j), ids);
}
}
return res;
} public Map<Integer, Set<String>> buildIdToEmails(List<List<String>> accounts) {
Map<Integer, Set<String>> idToEmails = new HashMap<>();
for (int i = 0; i < accounts.size(); i++) {
int root_id = find(i);
Set<String> emails = idToEmails.get(root_id);
if (emails == null) {
emails = new HashSet<>();
}
for (int j = 1; j < accounts.get(i).size(); j++) {
emails.add(accounts.get(i).get(j));
}
idToEmails.put(root_id, emails);
}
return idToEmails;
} public void union(int a, int b) {
int root_a = find(a);
int root_b = find(b);
if (root_a != root_b) {
father.put(root_a, root_b);
}
} public int find(int a) {
if (father.get(a) == a) {
return a;
}
Set<Integer> path = new HashSet<>();
int i = a;
while (father.get(i) != i) {
path.add(i);
i = father.get(i);
}
for (int num : path) {
father.put(num, i);
}
return i;
} }
Union Find - 20181102 - 20181105的更多相关文章
- Hive 数仓中常见的日期转换操作
(1)Hive 数仓中一些常用的dt与日期的转换操作 下面总结了自己工作中经常用到的一些日期转换,这类日期转换经常用于报表的时间粒度和统计周期的控制中 日期变换: (1)dt转日期 to_date(f ...
- SQL Server-聚焦UNIOL ALL/UNION查询(二十三)
前言 本节我们来看看有关查询中UNION和UNION ALL的问题,简短的内容,深入的理解,Always to review the basics. 初探UNION和UNION ALL 首先我们过一遍 ...
- SQL 提示介绍 hash/merge/concat union
查询提示一直是个很有争议的东西,因为他影响了sql server 自己选择执行计划.很多人在问是否应该使用查询提示的时候一般会被告知慎用或不要使用...但是个人认为善用提示在不修改语句的条件下,是常用 ...
- LINQ to SQL语句(8)之Concat/Union/Intersect/Except
适用场景:对两个集合的处理,例如追加.合并.取相同项.相交项等等. Concat(连接) 说明:连接不同的集合,不会自动过滤相同项:延迟. 1.简单形式: var q = ( from c in db ...
- SQLServer-----Union,Union All的使用方法
转载: http://blog.csdn.net/kiqinie/article/details/8132485 select a.Name from Material as a union sele ...
- 假如 UNION ALL 里面的子句 有 JOIN ,那个执行更快呢
比如: select id, name from table1 where name = 'x' union all select id, name from table2 where name = ...
- sql union和union all的用法及效率
UNION指令的目的是将两个SQL语句的结果合并起来.从这个角度来看, 我们会产生这样的感觉,UNION跟JOIN似乎有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION的一个限制是两个 ...
- 【oracle】union、union all、intersect、minus 的用法及区别
一.union与union all 首先建两个view create or replace view test_view_1 as as c from dual union as c from dua ...
- sql with as union all
WITH RPL (FId,Fname,Forder) AS ( SELECT ment.deptno,ment.deptname,ment.orderno FROM JTERP..fg_depart ...
随机推荐
- Part5核心初始化_lesson1---异常向量表
1.1异常 异常向量: 异常向量表: 代码的编写 start.S文件 gboot.lds链接器脚本文件 makefile工程文件:
- js选择文件夹路径
该方法只支持IE. 语法:strDir=Shell.BrowseForFolder(Hwnd,Title,Options,[RootFolder])参数:Hwnd:包含对话框的窗体句柄(handle) ...
- C# winform 打开新窗体 关闭当前窗体
Form1 的Button 下 { Form2 f2 = new Form2(); f2.ShowDialog(this);// this.Close(); } Form2 的load 下 { //只 ...
- super() 的入门使用
在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能,这时,我们就需要调用父类的方法了,可通过使用 super 来实现,比如: 1 2 3 4 5 ...
- golang 重构博客统计服务
欢迎关注楼主与他的小伙伴们的小站,每周分享一些技术文章,让我们在技术上一起成长------> 戳这里,欢迎光临小站 -_- 作为一个后端开发,在docker,etcd,k8s等新技术不断涌现的今 ...
- 《C#多线程编程实战》2.9 ReaderWirterLockSlim
可以多线程进行读写操作. 比如书上的示例代码是三个线程进行读取,两个线程进行写入工作. 如果 用之前学过的也不是不可以用,但是用的有些多. 所有ReaderWirterLockSlim专门为此而来. ...
- 强制所有网页链接在同一页面打开或者在TabControl中弹出新窗口
IEwebbrowser中老生常谈的话题. 一般的解决都是通过 // webBrowser.Navigating += WebBrowser_Navigating; 注册转跳前事件 private v ...
- 去掉textarea 右下角图标 resize: none;
如下图默认右下角有小图标: 加个样式: resize: none;就可以了:
- 洛谷P4014 分配问题(费用流)
传送门 可以把原图看做一个二分图,人在左边,任务在右边,求一个带权的最大和最小完美匹配 然而我并不会二分图做法,所以只好直接用费用流套进去,求一个最小费用最大流和最大费用最大流即可 //minamot ...
- The server of Apache (三)——网页优化
在企业中,部署apache后只采用默认的配置参数,会有很多问题,因为那些配置都是针对以前服务器配置的. 一.网页压缩 1.介绍 配置apache的网页压缩功能,是使用Gzip压缩算法来对apache服 ...