/** * 输入一个Long数组,按要求输出一个等长的Long数组 * 输出数组的元素值等于,输入数组除相同下标外其他元素的积 * 如:输入[1, 2, 3, 4], 输出[24, 12, 8, 6] * 输出数组:output[0] = input[1] * input[2] * input[3],即 24 = 2 * 3 * 4 * output[1] = input[0] * input[2] * input[3],即 12 = 1 * 3 * 4 * 要求: * 1. 需要在O(n)复杂度内完成操作 * 2. 不需要考虑乘积越界的问题 */ public class sk2 { public static void main(String[] args) { long [] x = {1, 2, 3, 2,5,6}; long [] xx =calc(x); for (int i = 0; i < xx.length; i++) { System.err.println(xx[i]); } } public static long[] calc(long[] array) { long[] l2 = null; if(array.length>0){ int arrayLength = array.length;//集合长度 l2 = new long [arrayLength] ; for (int i = 0; i < arrayLength; i++) { long x = 1; for (int j = 0; j < arrayLength; j++) { if(i != j) x*=array[j]; } l2[i]= x ; } } return l2; } }