modify code

pull/6/head
左程云 5 years ago
parent bf2c4ddc84
commit a7f7f0bf19

@ -28,22 +28,20 @@ public class Code01_MinPathSum {
if (m == null || m.length == 0 || m[0] == null || m[0].length == 0) {
return 0;
}
int more = Math.max(m.length, m[0].length); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD>Ǹ<EFBFBD>Ϊmore
int less = Math.min(m.length, m[0].length); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD>Ǹ<EFBFBD>Ϊless
boolean rowmore = more == m.length; // <20><><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><C7B2>Ǵ<EFBFBD><C7B4>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int[] arr = new int[less]; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3>Ƚ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>Сֵ
arr[0] = m[0][0];
for (int i = 1; i < less; i++) {
arr[i] = arr[i - 1] + (rowmore ? m[0][i] : m[i][0]);
int row = m.length;
int col = m[0].length;
int[] dp = new int[col];
dp[0] = m[0][0];
for (int j = 1; j < col; j++) {
dp[j] = dp[j - 1] + m[0][j];
}
for (int i = 1; i < more; i++) {
arr[0] = arr[0] + (rowmore ? m[i][0] : m[0][i]);
for (int j = 1; j < less; j++) {
arr[j] = Math.min(arr[j - 1], arr[j])
+ (rowmore ? m[i][j] : m[j][i]);
for (int i = 1; i < row; i++) {
dp[0] += m[i][0];
for (int j = 1; j < col; j++) {
dp[j] = Math.min(dp[j - 1], dp[j]) + m[i][j];
}
}
return arr[less - 1];
return dp[col - 1];
}
// for test
@ -54,7 +52,7 @@ public class Code01_MinPathSum {
int[][] result = new int[rowSize][colSize];
for (int i = 0; i != result.length; i++) {
for (int j = 0; j != result[0].length; j++) {
result[i][j] = (int) (Math.random() * 10);
result[i][j] = (int) (Math.random() * 100);
}
}
return result;
@ -71,10 +69,9 @@ public class Code01_MinPathSum {
}
public static void main(String[] args) {
// int[][] m = generateRandomMatrix(3, 4);
int[][] m = { { 1, 3, 5, 9 }, { 8, 1, 3, 4 }, { 5, 0, 6, 1 },
{ 8, 8, 4, 0 } };
printMatrix(m);
int rowSize = 10;
int colSize = 10;
int[][] m = generateRandomMatrix(rowSize, colSize);
System.out.println(minPathSum1(m));
System.out.println(minPathSum2(m));

@ -6,11 +6,12 @@ public class Code02_CoinsWayEveryPaperDifferent {
return process(arr, 0, aim);
}
// arr[index....] 组成正好rest这么多的钱有几种方法
public static int process(int[] arr, int index, int rest) {
if (rest < 0) {
return 0;
}
if (index == arr.length) {
if (index == arr.length) { // 没钱了!
return rest == 0 ? 1 : 0;
} else {
return process(arr, index + 1, rest) + process(arr, index + 1, rest - arr[index]);

@ -9,8 +9,9 @@ public class Code03_CoinsWayNoLimit {
return process(arr, 0, aim);
}
// arr[index....] 所有的面值每一个面值都可以任意选择张数组成正好rest这么多钱方法数多少
public static int process(int[] arr, int index, int rest) {
if (index == arr.length) {
if (index == arr.length) { // 没钱了
return rest == 0 ? 1 : 0;
}
int ways = 0;

@ -43,6 +43,8 @@ public class Code04_CoinsWaySameValueSamePapper {
return process(info.coins, info.zhangs, 0, aim);
}
// coins 面值数组,正数且去重
// zhangs 每种面值对应的张数
public static int process(int[] coins, int[] zhangs, int index, int rest) {
if (index == coins.length) {
return rest == 0 ? 1 : 0;

@ -1,18 +1,21 @@
package class22;
package class21;
public class Code01_BobDie {
public class Code05_BobDie {
public static double livePosibility1(int row, int col, int k, int N, int M) {
return (double) process(row, col, k, N, M) / Math.pow(4, k);
}
// 目前在rowcol位置还有rest步要走走完了如果还在棋盘中就获得1个生存点返回总的生存点数
public static long process(int row, int col, int rest, int N, int M) {
if (row < 0 || row == N || col < 0 || col == M) {
return 0;
}
// 还在棋盘中!
if (rest == 0) {
return 1;
}
// 还在棋盘中!还有步数要走
long up = process(row - 1, col, rest - 1, N, M);
long down = process(row + 1, col, rest - 1, N, M);
long left = process(row, col - 1, rest - 1, N, M);

@ -1,6 +1,6 @@
package class22;
public class Code02_KillMonster {
public class Code01_KillMonster {
public static double right1(int N, int M, int K) {
if (N < 1 || M < 1 || K < 1) {

@ -1,6 +1,6 @@
package class22;
public class Code03_MinCoinsNoLimit {
public class Code02_MinCoinsNoLimit {
public static int minCoins(int[] arr, int aim) {
return process(arr, 0, aim);

@ -1,8 +1,8 @@
package class23;
package class22;
import java.util.TreeSet;
public class Code01_SplitSumClosed {
public class Code03_SplitSumClosed {
public static int right(int[] arr) {
if (arr == null || arr.length < 2) {

@ -1,8 +1,8 @@
package class23;
package class22;
import java.util.TreeSet;
public class Code02_SplitSumClosedSizeHalf {
public class Code04_SplitSumClosedSizeHalf {
public static int right(int[] arr) {
if (arr == null || arr.length < 2) {

@ -1,6 +1,6 @@
package class23;
public class Code03_NQueens {
public class Code01_NQueens {
public static int num1(int n) {
if (n < 1) {

@ -1,10 +1,10 @@
package class22;
package class23;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.LinkedList;
public class Code04_MinCoinsOnePaper {
public class Code02_MinCoinsOnePaper {
public static int minCoins(int[] arr, int aim) {
return process(arr, 0, aim);
Loading…
Cancel
Save