标签:tin result content code 注意 off 剑指offer i+1 style
1 public class Solution { 2 public int[] multiply(int[] array) { 3 if(array.length <= 1) { 4 return new int[0]; 5 } 6 int[] result = new int[array.length]; 7 for(int i = 0; i < array.length; i ++) { 8 result[i] = 1; 9 for(int j = 0; j < array.length; 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 <= 1) { 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 < array.length; 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 < array.length; m ++) { 18 result[m] = left[m] * right[m]; 19 } 20 return result; 21 } 22 }
标签:tin result content code 注意 off 剑指offer i+1 style
原文地址:https://www.cnblogs.com/StringBuilder/p/14770605.html