└── algorithms └── week1 ├── analysis ├── BinarySearchThreeSum.java └── ThreeSum.java └── union ├── QuickFindUF.java ├── QuickUnionUF.java └── WeightedQuickUnionUF.java /algorithms/week1/analysis/BinarySearchThreeSum.java: -------------------------------------------------------------------------------- 1 | package algorithms.week1.analysis; 2 | 3 | import edu.princeton.cs.algs4.In; 4 | import edu.princeton.cs.algs4.StdOut; 5 | import edu.princeton.cs.algs4.Stopwatch; 6 | 7 | import java.util.Arrays; 8 | 9 | 10 | /** 11 | * @author Carlos 12 | * @description 三数求和(使用二分查找) 13 | * @Date 2019/9/11 14 | * 测试数据(相较暴力求解在大数据量有明显提升): 15 | * 16 | * D:\Java\Video_Git\out\production\Video_Git>java algorithms.week1.analysis.BinarySearchThreeSum 8ints.txt 17 | * 4 18 | * elapsed time = 0.002 19 | * 20 | * D:\Java\Video_Git\out\production\Video_Git>java algorithms.week1.analysis.BinarySearchThreeSum 1Kints.txt 21 | * 70 22 | * elapsed time = 0.035 23 | * 24 | * D:\Java\Video_Git\out\production\Video_Git>java algorithms.week1.analysis.BinarySearchThreeSum 2Kints.txt 25 | * 528 26 | * elapsed time = 0.175 27 | * 28 | * D:\Java\Video_Git\out\production\Video_Git>java algorithms.week1.analysis.BinarySearchThreeSum 4Kints.txt 29 | * 4039 30 | * elapsed time = 0.625 31 | * 32 | * D:\Java\Video_Git\out\production\Video_Git>java algorithms.week1.analysis.BinarySearchThreeSum 8Kints.txt 33 | * 32074 34 | * elapsed time = 2.382 35 | * 36 | * D:\Java\Video_Git\out\production\Video_Git>java algorithms.week1.analysis.BinarySearchThreeSum 16Kints.txt 37 | * 255181 38 | * elapsed time = 10.874 39 | * 40 | */ 41 | 42 | public class BinarySearchThreeSum { 43 | 44 | public static int binarySearch(int[] a, int key) { 45 | int lo = 0; 46 | int hi = a.length - 1; 47 | 48 | while (lo <= hi) { 49 | int mid = lo + (hi - lo) / 2; 50 | 51 | if (key > a[mid]) { 52 | lo = mid + 1; 53 | } else if (key < a[mid]) { 54 | hi = mid - 1; 55 | } else { 56 | return mid; 57 | } 58 | } 59 | 60 | return -1; 61 | } 62 | 63 | public static int count(int[] a) { 64 | int N = a.length; 65 | int count = 0; 66 | 67 | Arrays.sort(a); 68 | 69 | for (int i = 0; i < N; i++) { 70 | for (int j = i + 1; j < N; j++) { 71 | int result = binarySearch(a, -(a[i] + a[j])); 72 | if (result != -1 && result != i && result != j) { 73 | count++; 74 | } 75 | } 76 | } 77 | return count / 3; //同一组数据重复计算三次,除3为正确数量。 78 | } 79 | 80 | public static void main(String[] args) { 81 | int[] a = In.readInts(args[0]); 82 | Stopwatch stopwatch = new Stopwatch(); 83 | StdOut.println(count(a)); 84 | double time = stopwatch.elapsedTime(); 85 | StdOut.println("elapsed time = " + time); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /algorithms/week1/analysis/ThreeSum.java: -------------------------------------------------------------------------------- 1 | package algorithms.week1.analysis; 2 | 3 | import edu.princeton.cs.algs4.In; 4 | import edu.princeton.cs.algs4.StdOut; 5 | import edu.princeton.cs.algs4.Stopwatch; 6 | 7 | /** 8 | * @author Carlos 9 | * @description 三数之和(暴力算法) 10 | * @Date 2019/9/11 11 | * 12 | * 测试数据 13 | * 14 | * * D:\Java\Video_Git\out\production\Video_Git>java algorithms.week1.analysis.ThreeSum 8ints.txt 15 | * * 4 16 | * * elapsed time = 0.001 17 | * * 18 | * * D:\Java\Video_Git\out\production\Video_Git>java algorithms.week1.analysis.ThreeSum 1Kints.txt 19 | * * 70 20 | * * elapsed time = 0.224 21 | * * 22 | * * D:\Java\Video_Git\out\production\Video_Git>java algorithms.week1.analysis.ThreeSum 2Kints.txt 23 | * * 528 24 | * * elapsed time = 1.742 25 | * * 26 | * * D:\Java\Video_Git\out\production\Video_Git>java algorithms.week1.analysis.ThreeSum 4Kints.txt 27 | * * 4039 28 | * * elapsed time = 13.983 29 | * * 30 | * * 31 | */ 32 | 33 | public class ThreeSum { 34 | 35 | public static int count(int[] a) { 36 | int N = a.length; 37 | int count = 0; 38 | 39 | for(int i = 0; i < N; i++) { 40 | for (int j = i + 1; j < N; j++) { 41 | for (int k = j + 1; k < N; k++) { 42 | if (a[i] + a[j] + a[k] == 0) { 43 | count++; 44 | } 45 | } 46 | } 47 | } 48 | return count; 49 | } 50 | 51 | public static void main(String[] args) { 52 | int[] a = In.readInts(args[0]); 53 | Stopwatch stopwatch = new Stopwatch(); 54 | StdOut.println(count(a)); 55 | double time = stopwatch.elapsedTime(); 56 | StdOut.println("elapsed time = " + time); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /algorithms/week1/union/QuickFindUF.java: -------------------------------------------------------------------------------- 1 | package algorithms.week1.union; 2 | 3 | /** 4 | * @author Carlos 5 | * @description 快速查找 6 | * @Date 2019/9/5 7 | */ 8 | 9 | public class QuickFindUF { 10 | 11 | private int[] id; 12 | 13 | public QuickFindUF(int N) { 14 | id = new int[N]; 15 | for (int i = 0; i < N; i++) { 16 | id[i] = i; 17 | } 18 | } 19 | 20 | public boolean connected(int p, int q) { 21 | return id[p] == id[q]; 22 | } 23 | 24 | public void union(int p, int q) { 25 | int pid = id[p]; 26 | int qid = id[q]; 27 | 28 | for (int i = 0; i < id.length; i++) { 29 | if (id[i] == pid) { 30 | id[i] = qid; 31 | } 32 | } 33 | } 34 | 35 | public static void main(String[] args) { 36 | QuickFindUF uf = new QuickFindUF(8); 37 | System.out.println("1和2是否connected? : " + uf.connected(1, 2)); 38 | System.out.println("uf.union(1, 2)"); 39 | uf.union(1, 2); 40 | System.out.println("1和2是否connected? : " + uf.connected(1, 2)); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /algorithms/week1/union/QuickUnionUF.java: -------------------------------------------------------------------------------- 1 | package algorithms.week1.union; 2 | 3 | /** 4 | * @author Carlos 5 | * @description 快速合并 6 | * @Date 2019/9/5 7 | */ 8 | 9 | public class QuickUnionUF { 10 | 11 | private int[] id; 12 | 13 | public QuickUnionUF(int N) { 14 | id = new int[N]; 15 | for(int i = 0; i < N; i++) { 16 | id[i] = i; 17 | } 18 | } 19 | 20 | private int root(int i) { 21 | 22 | while (i != id[i]) { 23 | i = id[i]; 24 | } 25 | 26 | return i; 27 | } 28 | 29 | public boolean connected(int p, int q) { 30 | 31 | return root(p) == root(q); 32 | 33 | } 34 | 35 | public void union(int i, int j) { 36 | int pRoot = root(i); 37 | int qRoot = root(j); 38 | 39 | if (pRoot == qRoot) { 40 | return; 41 | } 42 | 43 | id[pRoot] = qRoot; 44 | } 45 | 46 | public static void main(String[] args) { 47 | QuickUnionUF uf = new QuickUnionUF(8); 48 | System.out.println("1和2是否connected? : " + uf.connected(1, 2)); 49 | System.out.println("uf.union(1, 2)"); 50 | uf.union(1, 2); 51 | System.out.println("1和2是否connected? : " + uf.connected(1, 2)); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /algorithms/week1/union/WeightedQuickUnionUF.java: -------------------------------------------------------------------------------- 1 | package algorithms.week1.union; 2 | 3 | /** 4 | * @author Carlos 5 | * @description 加权快速合并算法 6 | * @Date 2019/9/5 7 | */ 8 | 9 | public class WeightedQuickUnionUF { 10 | 11 | private int[] id; 12 | private int[] sz; 13 | 14 | public WeightedQuickUnionUF(int N) { 15 | id = new int[N]; 16 | sz = new int[N]; 17 | 18 | for (int i = 0; i < N; i++) { 19 | id[i] = i; 20 | sz[i] = 1; 21 | } 22 | } 23 | 24 | private int root(int i) { 25 | while(i != id[i]) { 26 | i = id[id[i]]; 27 | } 28 | return i; 29 | } 30 | 31 | public boolean connected(int p, int q) { 32 | return root(p) == root(q); 33 | } 34 | 35 | public void union(int p, int q) { 36 | int pRoot = root(p); 37 | int qRoot = root(q); 38 | 39 | if (pRoot == qRoot) { 40 | return; 41 | } 42 | 43 | if (sz[pRoot] > sz[qRoot]) { 44 | id[qRoot] = pRoot; 45 | sz[pRoot] += sz[qRoot]; 46 | } else { 47 | id[pRoot] = qRoot; 48 | sz[qRoot] += qRoot; 49 | } 50 | } 51 | 52 | public static void main(String[] args) { 53 | WeightedQuickUnionUF uf = new WeightedQuickUnionUF(8); 54 | System.out.println("1和2是否connected? : " + uf.connected(1, 2)); 55 | System.out.println("uf.union(1, 2)"); 56 | uf.union(1, 2); 57 | System.out.println("1和2是否connected? : " + uf.connected(1, 2)); 58 | } 59 | 60 | } 61 | --------------------------------------------------------------------------------