[剑指Offer][数组]构建乘积数组
2021-05-29 10:02
标签:tin result content code 注意 off 剑指offer i+1 style [剑指Offer][数组]构建乘积数组 标签:tin result content code 注意 off 剑指offer i+1 style 原文地址:https://www.cnblogs.com/StringBuilder/p/14770605.html题目描述
1 public class Solution {
2 public int[] multiply(int[] array) {
3 if(array.length ) {
4 return new int[0];
5 }
6 int[] result = new int[array.length];
7 for(int i = 0; i ) {
8 result[i] = 1;
9 for(int j = 0; j ) {
10 if(j == i) {
11 continue;
12 }
13 result[i] = result[i] * array[j];
14 if(result[i] == 0) {
15 break;
16 }
17 }
18 }
19 return result;
20 }
21 }
1 public class Solution {
2 public int[] multiply(int[] array) {
3 if(array.length ) {
4 return new int[0];
5 }
6 int[] result = new int[array.length];
7 int[] left = new int[array.length];
8 int[] right = new int[array.length];
9 left[0] = 1;
10 right[array.length - 1] = 1;
11 for(int i = 1; i ) {
12 left[i] = left[i - 1] * array[i - 1];
13 }
14 for(int j = array.length - 2; j >= 0; j --) {
15 right[j] = right[j + 1] * array[j + 1];
16 }
17 for(int m = 0; m ) {
18 result[m] = left[m] * right[m];
19 }
20 return result;
21 }
22 }
文章标题:[剑指Offer][数组]构建乘积数组
文章链接:http://soscw.com/index.php/essay/89061.html