package test;
public class Multiply {
public static void multiply(int[] array1, int[] array2){
int length1 = array1.length;
int length2 = array2.length;
if(length1 == length2 && length2 >1){
array2[0] =1;
for(int i=1; i<length1; ++i){
array2[i] = array2[i-1] * array1[i-1];
}
int temp =1;
for(int i = length1-2; i>=0; --i){
temp *= array1[i+1];
array2[i] *= temp;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums = {1,2,3,4};
int[] nums2 = new int[nums.length];
Multiply.multiply(nums, nums2);
for(int num : nums2){
System.out.print(num+" ");
}
}
}
剑指Offer 52题 构建乘积数组 Java版
最新推荐文章于 2021-02-18 21:24:32 发布