- 设计RandomPool结构
- 并查集
- 岛问题
1.设计RandomPool结构
题目要求: 设计一种结构,在该结构中有如下三个功能
insert(key):将某个key加入到该结构,做到不重复加入。
delete(key):将原本在结构中的某个key移除。
getRandom(): 等概率随机返回结构中的任何一个key。
public static class RandomPool<T>{
private final HashMap<T,Integer> hashMap1;
private final HashMap<Integer,T> hashMap2;
private int size;
public RandomPool() {
this.hashMap1 = new HashMap<>();
this.hashMap2 = new HashMap<>();
this.size = 0;
}
public void insert(T key){
//判断是否已经存在
if (hashMap1.containsKey(key)){
return;
}else {
hashMap1.put(key,size);
hashMap2.put(size++,key);
}
}
public void delete(T key){
//判断是否存在
if (hashMap1.containsKey(key)){
//得到数据
T last = hashMap2.get(--size); //最后一组数据的Key
Integer keyIndex = hashMap1.get(key); //要删除数据的value
//修改最后一组数据 的value 为要删除数据的 value
hashMap1.put(last,keyIndex);
hashMap2.put(keyIndex,last);
//删除要删除的数据
hashMap1.remove(key);
hashMap2.remove(size);
}
}
public T getRandom(){
if (size == 0){
return null;
}else {
return hashMap2.get((int)(Math.random() * size));
}
}
}
2.并查集
题目:
功能:
(1)查两个元素是否属于一个集合;
(2)将两个元素各自所在的集合合并为一个集合
(3)优化 在查询两个元素是否属于一个集合 将路过元素扁平化
public static class UnionFindSet{
private HashMap<Node,Node> fatherSet;
private HashMap<Node,Integer> sizeSet;
public UnionFindSet(List<Node> lists){
fatherSet = new HashMap<>();
sizeSet = new HashMap<>();
for (Node node:
lists) {
fatherSet.put(node,node); //开始时各自成集合
sizeSet.put(node,1); //开始时各自成集合size为1
}
}
//查找头结点 -> 非递归实现扁平化递归
private Node findHead_noRecursion(Node node) {
//记录当前结点
Node cur = node;
//记录当前结点的父结点
Node curFather = fatherSet.get(cur);
//记录最终的父结点
Node father;
while (curFather != cur){
cur = curFather;
curFather = fatherSet.get(cur);
}
father = curFather;
cur = node;
curFather = fatherSet.get(cur);
while (curFather != cur){
fatherSet.put(cur,father);
cur = curFather;
curFather = fatherSet.get(cur);
}
return father;
}
//查找头结点 -> 递归实现扁平化
private Node findHead_recursion(Node node) {
Node father = fatherSet.get(node);
if (father != node){
findHead_recursion(father);
}
fatherSet.put(node,father);
return father;
}
//查找是否属于同一集合
public boolean isSameSet(Node a, Node b) {
return findHead_recursion(a) == findHead_recursion(b);
}
//将不同的两个集合 合并
public void union(Node a, Node b){
if (a == null || b == null){
return;
}
Node aHead = findHead_recursion(a);
Node bHead = findHead_recursion(b);
if (aHead != bHead){
int aSize = sizeSet.get(aHead);
int bSize = sizeSet.get(bHead);
if (aSize > bSize){
fatherSet.put(bHead,aHead);
sizeSet.put(aHead,aSize + bSize);
sizeSet.remove(bHead);
}else {
fatherSet.put(bHead,aHead);
sizeSet.put(bHead,aSize + bSize);
sizeSet.remove(aHead);
}
}
}
}
3.岛问题
题目:
一个矩阵中只有0和1两种值,每个位置都可以和自己的上、下、左、右 四个位置相连,如果有一片1连在一起,这个部分叫做一个岛,求一个 矩阵中有多少个岛?
private static int countIslands(int[][] arr) {
//判空
if (arr == null || arr[0] == null) {
return 0;
}
int num = 0;
for (int i = 0;i < arr.length;i++){
for (int y = 0;y < arr[0].length;y++){
if (arr[i][y] == 1){
infect(arr,i,y);
num++;
}
}
}
return num;
}
private static void infect(int[][] arr,int row, int column) {
if (row < 0 || row >= arr.length || column < 0 || column >= arr[0].length || arr[row][column] != 1){
return;
}
arr[row][column] = 2;
//上
infect(arr,row - 1,column);
//下
infect(arr,row + 1,column);
//左
infect(arr,row,column - 1);
//右
infect(arr,row,column + 1);
}
使用并查集计算大数据
(1)先将原矩阵 分成多块
(2)每块调用singleCPU求岛数量
-》其中每一个感染源 为并查集头结点
(3)看边界条件
-》如果边界两点不来自同一个岛屿 -》 合并 岛数量-1
-》如果边界两个点来自同一个岛屿 -》 不管