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 ...
随机推荐
- Luogu 3206 [HNOI2010]城市建设
BZOJ 2001 很神仙的cdq分治 先放论文的链接 顾昱洲_浅谈一类分治算法 我们考虑分治询问,用$solve(l, r)$表示询问编号在$[l, r]$时的情况,那么当$l == r$的时候 ...
- Luogu 3241 [HNOI2015]开店
BZOJ 4012权限题 浙科协的网突然炸了,好慌…… 据说正解是动态点分治,然而我并不会,我选择树链剖分 + 主席树维护. 设$dis_i$表示$i$到$root(1)$的值,那么对于一个询问$u$ ...
- Cookie存中文乱码的问题
有个奇怪的问题:登录页面中使用Cookie存值,Cookie中要存中文汉字.代码在本地调试,一切OK,汉字也能顺利存到Cookie和从Cookie中读出,但是放到服务器上不管用了,好好的汉字成了乱码, ...
- (转)Web API 入门指南 - 闲话安全
原文地址:http://www.cnblogs.com/developersupport/p/WebAPI-Security.html Web API入门指南有些朋友回复问了些安全方面的问题,安全方面 ...
- C#字符串string的常用使用方法(转载)
1--->字符串的声明: 1.string s=new string(char[] arr) //根据一个字符数组声明字符串,即将字符字组转化为字符串. 2.string s=new s ...
- 有大佬拉我一把麽,现在广州还有c++后台实习招聘麽
有大佬拉我一把麽,现在广州还有c++后台实习招聘麽
- openstack组件服务的入口寻找方法
在centos7系统上,安装openstack服务以后,可以通过以下命令,查找到该系统上,已经安装的openstack服务 [root@xzto01n010027244133 ~]# systemct ...
- 《spring 攻略》笔记1
chapter1 spring简介 两种spring ioc容器实现类型: BeanFactory ApplicationContext 应用程序上下文 DI技巧: @Autowired(requir ...
- 看了这篇Dubbo RPC面试题,让天下没有难面的面试题!
前言: RPC非常重要,很多人面试的时候都挂在了这个地方!你要是还不懂RPC是什么?他的基本原理是什么?你一定要把下边的内容记起来!好好研究一下!特别是文中给出的一张关于RPC的基本流程图,重点中 ...
- 在一个java类里,private int a; 什么时候要使用integer
private Integer index; if(index == null) index = 0; else this.index = index; Integer有一个明显的好处,就是它能比in ...