Solution URL
https://leetcode.com/submissions/detail/424010915/
代码
class Solution {
public int[] productExceptSelf(int[] nums) {
//ans: solution
//time: O(N)
//space: O(N)
int[] left = new int[nums.length];
int[] right = new int[nums.length];
//initalize the left and right array
left[0] = 1;
right[nums.length - 1] = 1;
//calculate the left
for(int i = 1; i < nums.length; i++){
left[i] = left[i - 1] * nums[i - 1];
}
//calculate the right
for(int i = nums.length - 2; i >= 0; i--){
right[i] = right[i + 1] * nums[i + 1];
}
for(int i = 0; i < nums.length; i++){
nums[i] = left[i] * right[i];
}
return nums;
}
}