238. Product of Array Except Self
先存这个数左边的乘机 然后从尾部向前面扫 并且计算着每个数右边所有数的乘机 叠加到原来的结果上和以前遇到的一个装多少水的题思路很像
public class Solution {
//左边右边分别扫的思想
public int[] productExceptSelf(int[] nums) {
int[] res = new int[nums.length];
res[0] = 1;
for(int i = 1;i < nums.length;i++){
res[i] = res[i-1]*nums[i-1];
}
int right = 1;
//from end to start and calculate the right part result in the meantime
for(int i = nums.length - 1;i >= 0;i--){
res[i] = right*res[i];
right = right * nums[i];
}
return res;
}
}