From 15373ef31f45a6ce652eb7ffde6a5e12078fe5b3 Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Mon, 12 Mar 2018 09:34:03 +0800 Subject: [PATCH] auto commit --- notes/剑指 offer 题解.md | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/notes/剑指 offer 题解.md b/notes/剑指 offer 题解.md index 8206b36d..8aa25158 100644 --- a/notes/剑指 offer 题解.md +++ b/notes/剑指 offer 题解.md @@ -2052,21 +2052,12 @@ public int Add(int num1, int num2) { ```java public int[] multiply(int[] A) { int n = A.length; - int[][] dp = new int[n][n]; - for (int i = 0; i < n; i++) { - dp[i][i] = A[i]; - } - for (int i = 0; i < n; i++) { - for (int j = i + 1; j < n; j++) { - dp[i][j] = dp[i][j - 1] * A[j]; - } - } - int[] B = new int[n]; - Arrays.fill(B, 1); - for (int i = 0; i < n; i++) { - if (i != 0) B[i] *= dp[0][i - 1]; - if (i != n - 1) B[i] *= dp[i + 1][n - 1]; + for (int i = 0, product = 1; i < n; product *= A[i], i++) { + B[i] = product; + } + for (int i = n - 1, product = 1; i >= 0; product *= A[i], i--) { + B[i] *= product; } return B; }