parent
0106bd9d69
commit
745f7e0535
@ -0,0 +1,186 @@
|
|||||||
|
package class26;
|
||||||
|
|
||||||
|
public class Code01_FibonacciProblem {
|
||||||
|
|
||||||
|
public static int f1(int n) {
|
||||||
|
if (n < 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (n == 1 || n == 2) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return f1(n - 1) + f1(n - 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int f2(int n) {
|
||||||
|
if (n < 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (n == 1 || n == 2) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int res = 1;
|
||||||
|
int pre = 1;
|
||||||
|
int tmp = 0;
|
||||||
|
for (int i = 3; i <= n; i++) {
|
||||||
|
tmp = res;
|
||||||
|
res = res + pre;
|
||||||
|
pre = tmp;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int f3(int n) {
|
||||||
|
if (n < 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (n == 1 || n == 2) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// [ 1 ,1 ]
|
||||||
|
// [ 1, 0 ]
|
||||||
|
int[][] base = {
|
||||||
|
{ 1, 1 },
|
||||||
|
{ 1, 0 }
|
||||||
|
};
|
||||||
|
int[][] res = matrixPower(base, n - 2);
|
||||||
|
return res[0][0] + res[1][0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[][] matrixPower(int[][] m, int p) {
|
||||||
|
int[][] res = new int[m.length][m[0].length];
|
||||||
|
for (int i = 0; i < res.length; i++) {
|
||||||
|
res[i][i] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// res = 矩阵中的1
|
||||||
|
int[][] tmp = m;// 矩阵1次方
|
||||||
|
for (; p != 0; p >>= 1) {
|
||||||
|
if ((p & 1) != 0) {
|
||||||
|
res = muliMatrix(res, tmp);
|
||||||
|
}
|
||||||
|
tmp = muliMatrix(tmp, tmp);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 两个矩阵乘完之后的结果返回
|
||||||
|
public static int[][] muliMatrix(int[][] m1, int[][] m2) {
|
||||||
|
int[][] res = new int[m1.length][m2[0].length];
|
||||||
|
for (int i = 0; i < m1.length; i++) {
|
||||||
|
for (int j = 0; j < m2[0].length; j++) {
|
||||||
|
for (int k = 0; k < m2.length; k++) {
|
||||||
|
res[i][j] += m1[i][k] * m2[k][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int s1(int n) {
|
||||||
|
if (n < 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (n == 1 || n == 2) {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
return s1(n - 1) + s1(n - 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int s2(int n) {
|
||||||
|
if (n < 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (n == 1 || n == 2) {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
int res = 2;
|
||||||
|
int pre = 1;
|
||||||
|
int tmp = 0;
|
||||||
|
for (int i = 3; i <= n; i++) {
|
||||||
|
tmp = res;
|
||||||
|
res = res + pre;
|
||||||
|
pre = tmp;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int s3(int n) {
|
||||||
|
if (n < 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (n == 1 || n == 2) {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
int[][] base = { { 1, 1 }, { 1, 0 } };
|
||||||
|
int[][] res = matrixPower(base, n - 2);
|
||||||
|
return 2 * res[0][0] + res[1][0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int c1(int n) {
|
||||||
|
if (n < 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (n == 1 || n == 2 || n == 3) {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
return c1(n - 1) + c1(n - 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int c2(int n) {
|
||||||
|
if (n < 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (n == 1 || n == 2 || n == 3) {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
int res = 3;
|
||||||
|
int pre = 2;
|
||||||
|
int prepre = 1;
|
||||||
|
int tmp1 = 0;
|
||||||
|
int tmp2 = 0;
|
||||||
|
for (int i = 4; i <= n; i++) {
|
||||||
|
tmp1 = res;
|
||||||
|
tmp2 = pre;
|
||||||
|
res = res + prepre;
|
||||||
|
pre = tmp1;
|
||||||
|
prepre = tmp2;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int c3(int n) {
|
||||||
|
if (n < 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (n == 1 || n == 2 || n == 3) {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
int[][] base = {
|
||||||
|
{ 1, 1, 0 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 1, 0, 0 } };
|
||||||
|
int[][] res = matrixPower(base, n - 3);
|
||||||
|
return 3 * res[0][0] + 2 * res[1][0] + res[2][0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int n = 19;
|
||||||
|
System.out.println(f1(n));
|
||||||
|
System.out.println(f2(n));
|
||||||
|
System.out.println(f3(n));
|
||||||
|
System.out.println("===");
|
||||||
|
|
||||||
|
System.out.println(s1(n));
|
||||||
|
System.out.println(s2(n));
|
||||||
|
System.out.println(s3(n));
|
||||||
|
System.out.println("===");
|
||||||
|
|
||||||
|
System.out.println(c1(n));
|
||||||
|
System.out.println(c2(n));
|
||||||
|
System.out.println(c3(n));
|
||||||
|
System.out.println("===");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,109 @@
|
|||||||
|
package class26;
|
||||||
|
|
||||||
|
public class Code02_ZeroLeftOneStringNumber {
|
||||||
|
|
||||||
|
public static int getNum1(int n) {
|
||||||
|
if (n < 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return process(1, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int process(int i, int n) {
|
||||||
|
if (i == n - 1) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
if (i == n) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return process(i + 1, n) + process(i + 2, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getNum2(int n) {
|
||||||
|
if (n < 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (n == 1) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int pre = 1;
|
||||||
|
int cur = 1;
|
||||||
|
int tmp = 0;
|
||||||
|
for (int i = 2; i < n + 1; i++) {
|
||||||
|
tmp = cur;
|
||||||
|
cur += pre;
|
||||||
|
pre = tmp;
|
||||||
|
}
|
||||||
|
return cur;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getNum3(int n) {
|
||||||
|
if (n < 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (n == 1 || n == 2) {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
int[][] base = { { 1, 1 }, { 1, 0 } };
|
||||||
|
int[][] res = matrixPower(base, n - 2);
|
||||||
|
return 2 * res[0][0] + res[1][0];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static int fi(int n) {
|
||||||
|
if (n < 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (n == 1 || n == 2) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int[][] base = { { 1, 1 },
|
||||||
|
{ 1, 0 } };
|
||||||
|
int[][] res = matrixPower(base, n - 2);
|
||||||
|
return res[0][0] + res[1][0];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static int[][] matrixPower(int[][] m, int p) {
|
||||||
|
int[][] res = new int[m.length][m[0].length];
|
||||||
|
for (int i = 0; i < res.length; i++) {
|
||||||
|
res[i][i] = 1;
|
||||||
|
}
|
||||||
|
int[][] tmp = m;
|
||||||
|
for (; p != 0; p >>= 1) {
|
||||||
|
if ((p & 1) != 0) {
|
||||||
|
res = muliMatrix(res, tmp);
|
||||||
|
}
|
||||||
|
tmp = muliMatrix(tmp, tmp);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[][] muliMatrix(int[][] m1, int[][] m2) {
|
||||||
|
int[][] res = new int[m1.length][m2[0].length];
|
||||||
|
for (int i = 0; i < m1.length; i++) {
|
||||||
|
for (int j = 0; j < m2[0].length; j++) {
|
||||||
|
for (int k = 0; k < m2.length; k++) {
|
||||||
|
res[i][j] += m1[i][k] * m2[k][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
for (int i = 0; i != 20; i++) {
|
||||||
|
System.out.println(getNum1(i));
|
||||||
|
System.out.println(getNum2(i));
|
||||||
|
System.out.println(getNum3(i));
|
||||||
|
System.out.println("===================");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
package class27;
|
||||||
|
|
||||||
|
public class Code03_ReservoirSampling {
|
||||||
|
|
||||||
|
public static class RandomBox {
|
||||||
|
private int[] bag;
|
||||||
|
private int N;
|
||||||
|
private int count;
|
||||||
|
|
||||||
|
public RandomBox(int capacity) {
|
||||||
|
bag = new int[capacity];
|
||||||
|
N = capacity;
|
||||||
|
count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int rand(int max) {
|
||||||
|
return (int) (Math.random() * max) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(int num) {
|
||||||
|
count++;
|
||||||
|
if (count <= N) {
|
||||||
|
bag[count - 1] = num;
|
||||||
|
} else {
|
||||||
|
if (rand(count) <= N) {
|
||||||
|
bag[rand(N) - 1] = num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] choices() {
|
||||||
|
int[] ans = new int[N];
|
||||||
|
for (int i = 0; i < N; i++) {
|
||||||
|
ans[i] = bag[i];
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("hello");
|
||||||
|
int all = 100;
|
||||||
|
int choose = 10;
|
||||||
|
int testTimes = 50000;
|
||||||
|
int[] counts = new int[all + 1];
|
||||||
|
for (int i = 0; i < testTimes; i++) {
|
||||||
|
RandomBox box = new RandomBox(choose);
|
||||||
|
for (int num = 1; num <= all; num++) {
|
||||||
|
box.add(num);
|
||||||
|
}
|
||||||
|
int[] ans = box.choices();
|
||||||
|
for (int j = 0; j < ans.length; j++) {
|
||||||
|
counts[ans[j]]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < counts.length; i++) {
|
||||||
|
System.out.println(i + " times : " + counts[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
package class28;
|
||||||
|
|
||||||
|
public class Code03_IsRotation {
|
||||||
|
|
||||||
|
public static boolean isRotation(String a, String b) {
|
||||||
|
if (a == null || b == null || a.length() != b.length()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String b2 = b + b;
|
||||||
|
return getIndexOf(b2, a) != -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// KMP Algorithm
|
||||||
|
public static int getIndexOf(String s, String m) {
|
||||||
|
if (s.length() < m.length()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
char[] ss = s.toCharArray();
|
||||||
|
char[] ms = m.toCharArray();
|
||||||
|
int si = 0;
|
||||||
|
int mi = 0;
|
||||||
|
int[] next = getNextArray(ms);
|
||||||
|
while (si < ss.length && mi < ms.length) {
|
||||||
|
if (ss[si] == ms[mi]) {
|
||||||
|
si++;
|
||||||
|
mi++;
|
||||||
|
} else if (next[mi] == -1) {
|
||||||
|
si++;
|
||||||
|
} else {
|
||||||
|
mi = next[mi];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mi == ms.length ? si - mi : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[] getNextArray(char[] ms) {
|
||||||
|
if (ms.length == 1) {
|
||||||
|
return new int[] { -1 };
|
||||||
|
}
|
||||||
|
int[] next = new int[ms.length];
|
||||||
|
next[0] = -1;
|
||||||
|
next[1] = 0;
|
||||||
|
int pos = 2;
|
||||||
|
int cn = 0;
|
||||||
|
while (pos < next.length) {
|
||||||
|
if (ms[pos - 1] == ms[cn]) {
|
||||||
|
next[pos++] = ++cn;
|
||||||
|
} else if (cn > 0) {
|
||||||
|
cn = next[cn];
|
||||||
|
} else {
|
||||||
|
next[pos++] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String str1 = "yunzuocheng";
|
||||||
|
String str2 = "zuochengyun";
|
||||||
|
System.out.println(isRotation(str1, str2));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
package class29;
|
||||||
|
|
||||||
|
public class Code02_AddShortestEnd {
|
||||||
|
|
||||||
|
public static String shortestEnd(String s) {
|
||||||
|
if (s == null || s.length() == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
char[] str = manacherString(s);
|
||||||
|
int[] pArr = new int[str.length];
|
||||||
|
int C = -1;
|
||||||
|
int R = -1;
|
||||||
|
int maxContainsEnd = -1;
|
||||||
|
for (int i = 0; i != str.length; i++) {
|
||||||
|
pArr[i] = R > i ? Math.min(pArr[2 * C - i], R - i) : 1;
|
||||||
|
while (i + pArr[i] < str.length && i - pArr[i] > -1) {
|
||||||
|
if (str[i + pArr[i]] == str[i - pArr[i]])
|
||||||
|
pArr[i]++;
|
||||||
|
else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i + pArr[i] > R) {
|
||||||
|
R = i + pArr[i];
|
||||||
|
C = i;
|
||||||
|
}
|
||||||
|
if (R == str.length) {
|
||||||
|
maxContainsEnd = pArr[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
char[] res = new char[s.length() - maxContainsEnd + 1];
|
||||||
|
for (int i = 0; i < res.length; i++) {
|
||||||
|
res[res.length - 1 - i] = str[i * 2 + 1];
|
||||||
|
}
|
||||||
|
return String.valueOf(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static char[] manacherString(String str) {
|
||||||
|
char[] charArr = str.toCharArray();
|
||||||
|
char[] res = new char[str.length() * 2 + 1];
|
||||||
|
int index = 0;
|
||||||
|
for (int i = 0; i != res.length; i++) {
|
||||||
|
res[i] = (i & 1) == 0 ? '#' : charArr[index++];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String str1 = "abcd123321";
|
||||||
|
System.out.println(shortestEnd(str1));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,117 @@
|
|||||||
|
package class30;
|
||||||
|
|
||||||
|
public class Code05_MinHeight {
|
||||||
|
|
||||||
|
public static class Node {
|
||||||
|
public int val;
|
||||||
|
public Node left;
|
||||||
|
public Node right;
|
||||||
|
|
||||||
|
public Node(int x) {
|
||||||
|
val = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int minHeight1(Node head) {
|
||||||
|
if (head == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return p(head);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int p(Node x) {
|
||||||
|
if (x.left == null && x.right == null) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// 左右子树起码有一个不为空
|
||||||
|
int leftH = Integer.MAX_VALUE;
|
||||||
|
if (x.left != null) {
|
||||||
|
leftH = p(x.left);
|
||||||
|
}
|
||||||
|
int rightH = Integer.MAX_VALUE;
|
||||||
|
if (x.right != null) {
|
||||||
|
rightH = p(x.right);
|
||||||
|
}
|
||||||
|
return 1 + Math.min(leftH, rightH);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据morris遍历改写
|
||||||
|
public static int minHeight2(Node head) {
|
||||||
|
if (head == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
Node cur = head;
|
||||||
|
Node mostRight = null;
|
||||||
|
int curLevel = 0;
|
||||||
|
int minHeight = Integer.MAX_VALUE;
|
||||||
|
while (cur != null) {
|
||||||
|
mostRight = cur.left;
|
||||||
|
if (mostRight != null) {
|
||||||
|
int rightBoardSize = 1;
|
||||||
|
while (mostRight.right != null && mostRight.right != cur) {
|
||||||
|
rightBoardSize++;
|
||||||
|
mostRight = mostRight.right;
|
||||||
|
}
|
||||||
|
if (mostRight.right == null) { // 第一次到达
|
||||||
|
curLevel++;
|
||||||
|
mostRight.right = cur;
|
||||||
|
cur = cur.left;
|
||||||
|
continue;
|
||||||
|
} else { // 第二次到达
|
||||||
|
if (mostRight.left == null) {
|
||||||
|
minHeight = Math.min(minHeight, curLevel);
|
||||||
|
}
|
||||||
|
curLevel -= rightBoardSize;
|
||||||
|
mostRight.right = null;
|
||||||
|
}
|
||||||
|
} else { // 只有一次到达
|
||||||
|
curLevel++;
|
||||||
|
}
|
||||||
|
cur = cur.right;
|
||||||
|
}
|
||||||
|
int finalRight = 1;
|
||||||
|
cur = head;
|
||||||
|
while (cur.right != null) {
|
||||||
|
finalRight++;
|
||||||
|
cur = cur.right;
|
||||||
|
}
|
||||||
|
if (cur.left == null && cur.right == null) {
|
||||||
|
minHeight = Math.min(minHeight, finalRight);
|
||||||
|
}
|
||||||
|
return minHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
// for test
|
||||||
|
public static Node generateRandomBST(int maxLevel, int maxValue) {
|
||||||
|
return generate(1, maxLevel, maxValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
// for test
|
||||||
|
public static Node generate(int level, int maxLevel, int maxValue) {
|
||||||
|
if (level > maxLevel || Math.random() < 0.5) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Node head = new Node((int) (Math.random() * maxValue));
|
||||||
|
head.left = generate(level + 1, maxLevel, maxValue);
|
||||||
|
head.right = generate(level + 1, maxLevel, maxValue);
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int treeLevel = 7;
|
||||||
|
int nodeMaxValue = 5;
|
||||||
|
int testTimes = 100000;
|
||||||
|
System.out.println("test begin");
|
||||||
|
for (int i = 0; i < testTimes; i++) {
|
||||||
|
Node head = generateRandomBST(treeLevel, nodeMaxValue);
|
||||||
|
int ans1 = minHeight1(head);
|
||||||
|
int ans2 = minHeight2(head);
|
||||||
|
if (ans1 != ans2) {
|
||||||
|
System.out.println("Oops!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("test finish!");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue