题目描述:
解析:因为考虑有0的存在,所以不能除法 解题思路:先从左到右乘一遍,再从右到左乘一遍
class Solution {
public int[] productExceptSelf(int[] nums) {
/**
* 因为考虑有0的存在,所以不能除法
* 解题思路:先从左到右乘一遍,再从右到左乘一遍
*/
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 r=1;
for (int j = nums.length-1; j > -1; j--) {
res[j]*=r;
r*=nums[j];
}
return res;
}
}