From cb86646344fcd7a42ed63e12139b31ffcce16274 Mon Sep 17 00:00:00 2001 From: algorithmzuo Date: Wed, 6 Apr 2022 15:00:00 +0800 Subject: [PATCH] modify code --- src/class26/Code02_FibonacciProblem.java | 21 ++++++++++-------- .../Code03_ZeroLeftOneStringNumber.java | 22 +++++++++++-------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/class26/Code02_FibonacciProblem.java b/src/class26/Code02_FibonacciProblem.java index 248c731..7f50420 100644 --- a/src/class26/Code02_FibonacciProblem.java +++ b/src/class26/Code02_FibonacciProblem.java @@ -57,24 +57,27 @@ public class Code02_FibonacciProblem { int[][] t = m;// 矩阵1次方 for (; p != 0; p >>= 1) { if ((p & 1) != 0) { - res = muliMatrix(res, t); + res = product(res, t); } - t = muliMatrix(t, t); + t = product(t, t); } 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]; + public static int[][] product(int[][] a, int[][] b) { + int n = a.length; + int m = b[0].length; + int k = a[0].length; // a的列数同时也是b的行数 + int[][] ans = new int[n][m]; + for(int i = 0 ; i < n; i++) { + for(int j = 0 ; j < m;j++) { + for(int c = 0; c < k; c++) { + ans[i][j] += a[i][c] * b[c][j]; } } } - return res; + return ans; } public static int s1(int n) { diff --git a/src/class26/Code03_ZeroLeftOneStringNumber.java b/src/class26/Code03_ZeroLeftOneStringNumber.java index b21906e..8cc3ce2 100644 --- a/src/class26/Code03_ZeroLeftOneStringNumber.java +++ b/src/class26/Code03_ZeroLeftOneStringNumber.java @@ -78,23 +78,27 @@ public class Code03_ZeroLeftOneStringNumber { int[][] tmp = m; for (; p != 0; p >>= 1) { if ((p & 1) != 0) { - res = muliMatrix(res, tmp); + res = product(res, tmp); } - tmp = muliMatrix(tmp, tmp); + tmp = product(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]; + // 两个矩阵乘完之后的结果返回 + public static int[][] product(int[][] a, int[][] b) { + int n = a.length; + int m = b[0].length; + int k = a[0].length; // a的列数同时也是b的行数 + int[][] ans = new int[n][m]; + for(int i = 0 ; i < n; i++) { + for(int j = 0 ; j < m;j++) { + for(int c = 0; c < k; c++) { + ans[i][j] += a[i][c] * b[c][j]; } } } - return res; + return ans; } public static void main(String[] args) {