Product of Array Exclude Itself 数组剔除元素后的乘积
Description
Given an integers array A.
Define B[i] = A[0] * … * A[i-1] * A[i+1] * … * A[n-1]B[i]=A[0]∗…∗A[i−1]∗A[i+1]∗…∗A[n−1], calculate B WITHOUT divide operation.Out put B
public class Solution {
/*
* @param nums: Given an integers array A
* @return: A long long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]
*/
public List<Long> productExcludeItself(List<Integer> nums) {
// write your code here
List<Long> result = new ArrayList<Long>(nums.size());
int n = nums.size() ;
for(int i = 0 ; i < n ; i++){
long s = 1 ;
for(int j = 0 ; j < n ; j++){
if(j == i){
continue ;
}
s = s* nums.get(j) ;
}
result.add(i, s) ;
}
return result ;
}
}