更新upstream
This commit is contained in:
@ -0,0 +1,41 @@
|
||||
package com.raorao.java.althorithm.union;
|
||||
|
||||
/**
|
||||
* 查找优先的并查集.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-27-9:47
|
||||
*/
|
||||
public class QuickFindUF extends UF {
|
||||
|
||||
public QuickFindUF(int N) {
|
||||
super(N);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int find(int p) {
|
||||
if (p >= id.length) {
|
||||
return -1;
|
||||
}
|
||||
return id[p];
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void union(int p, int q) {
|
||||
|
||||
int pID = find(p);
|
||||
int qID = find(q);
|
||||
|
||||
if (pID == qID) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < id.length; i++) {
|
||||
if (id[i] == pID) {
|
||||
id[i] = qID;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.raorao.java.althorithm.union;
|
||||
|
||||
/**
|
||||
* 合并优先.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-27-9:50
|
||||
*/
|
||||
public class QuickUnionUF extends UF {
|
||||
|
||||
public QuickUnionUF(int N) {
|
||||
super(N);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int find(int p) {
|
||||
if (p >= id.length) {
|
||||
return -1;
|
||||
}
|
||||
while (p != id[p]) {
|
||||
p = id[p];
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void union(int p, int q) {
|
||||
|
||||
int pRoot = find(p);
|
||||
int qRoot = find(q);
|
||||
|
||||
if (pRoot != qRoot) {
|
||||
id[pRoot] = qRoot;
|
||||
}
|
||||
}
|
||||
}
|
27
code/src/main/java/com/raorao/java/althorithm/union/UF.java
Normal file
27
code/src/main/java/com/raorao/java/althorithm/union/UF.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.raorao.java.althorithm.union;
|
||||
|
||||
/**
|
||||
* 并查集.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-27-9:46
|
||||
*/
|
||||
public abstract class UF {
|
||||
|
||||
protected int[] id;
|
||||
|
||||
public UF(int N) {
|
||||
id = new int[N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
id[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean connected(int p, int q) {
|
||||
return find(p) == find(q);
|
||||
}
|
||||
|
||||
public abstract int find(int p);
|
||||
|
||||
public abstract void union(int p, int q);
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.raorao.java.althorithm.union;
|
||||
|
||||
/**
|
||||
* 并查集测试.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-27-9:55
|
||||
*/
|
||||
public class UFMain {
|
||||
|
||||
public static void main(String[] args) {
|
||||
UF uf = new QuickFindUF(10);
|
||||
uf.union(1, 2);
|
||||
uf.union(3, 4);
|
||||
uf.union(0, 9);
|
||||
uf.union(4, 7);
|
||||
uf.union(6, 5);
|
||||
uf.union(5, 8);
|
||||
uf.union(3, 9);
|
||||
uf.union(1, 8);
|
||||
|
||||
System.out.println(uf.find(0)); // 9
|
||||
System.out.println(uf.find(1)); // 5
|
||||
System.out.println(uf.find(2)); // 5
|
||||
System.out.println(uf.find(3)); // 9
|
||||
System.out.println(uf.find(4)); // 9
|
||||
System.out.println(uf.find(5)); // 5
|
||||
System.out.println(uf.find(6)); // 5
|
||||
System.out.println(uf.find(7)); // 9
|
||||
System.out.println(uf.find(8)); // 5
|
||||
System.out.println(uf.find(9)); // 9
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.raorao.java.althorithm.union;
|
||||
|
||||
/**
|
||||
* 加权快速合并.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-27-9:52
|
||||
*/
|
||||
public class WeightedQuickUnionUF extends UF {
|
||||
|
||||
// 保存节点的数量信息
|
||||
private int[] sz;
|
||||
|
||||
public WeightedQuickUnionUF(int N) {
|
||||
super(N);
|
||||
this.sz = new int[N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
this.sz[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int find(int p) {
|
||||
if (p >= id.length) {
|
||||
return -1;
|
||||
}
|
||||
while (p != id[p]) {
|
||||
p = id[p];
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void union(int p, int q) {
|
||||
|
||||
int i = find(p);
|
||||
int j = find(q);
|
||||
|
||||
if (i == j) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (sz[i] < sz[j]) {
|
||||
id[i] = j;
|
||||
sz[j] += sz[i];
|
||||
} else {
|
||||
id[j] = i;
|
||||
sz[i] += sz[j];
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.raorao.leetcode.q039;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 组合求和.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-27-10:44
|
||||
*/
|
||||
public class CombinationSum {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] candidates = new int[] {2, 3, 6, 7};
|
||||
int target = 7;
|
||||
List<List<Integer>> res = new CombinationSum().combinationSum(candidates, target);
|
||||
res.forEach(e -> {
|
||||
e.forEach(f -> System.out.print(f + " "));
|
||||
System.out.println();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public List<List<Integer>> combinationSum(int[] candidates, int target) {
|
||||
List<List<Integer>> sums = new ArrayList<>();
|
||||
backTracking(candidates, new ArrayList<>(), sums, 0, target);
|
||||
return sums;
|
||||
}
|
||||
|
||||
private void backTracking(int[] candidates, ArrayList<Integer> tempList, List<List<Integer>> sums,
|
||||
int start, int target) {
|
||||
if (target == 0) {
|
||||
sums.add(new ArrayList<>(tempList));
|
||||
return;
|
||||
}
|
||||
for (int i = start; i < candidates.length; i++) {
|
||||
if (candidates[i] <= target) {
|
||||
tempList.add(candidates[i]);
|
||||
backTracking(candidates, tempList, sums, i , target - candidates[i]);
|
||||
tempList.remove(tempList.size() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.raorao.leetcode.q077;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 组合数.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-27-10:29
|
||||
*/
|
||||
public class Combinations {
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<List<Integer>> res = new Combinations().combine(4, 2);
|
||||
res.forEach(e -> {
|
||||
e.forEach(f -> System.out.print(f + " "));
|
||||
System.out.println();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public List<List<Integer>> combine(int n, int k) {
|
||||
List<List<Integer>> combinations = new ArrayList<>();
|
||||
List<Integer> combineList = new ArrayList<>();
|
||||
backtracking(combineList, combinations, 1, k, n);
|
||||
return combinations;
|
||||
}
|
||||
|
||||
private void backtracking(List<Integer> combineList, List<List<Integer>> combinations, int start,
|
||||
int k, final int n) {
|
||||
if (k == 0) {
|
||||
combinations.add(new ArrayList<>(combineList));
|
||||
return;
|
||||
}
|
||||
for (int i = start; i <= n - k + 1; i++) { // 剪枝
|
||||
combineList.add(i);
|
||||
backtracking(combineList, combinations, i + 1, k - 1, n);
|
||||
combineList.remove(combineList.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user