Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int[] productExceptSelf(int[] nums) {
- int[] ans = new int[nums.length];
- Arrays.fill(ans, 1);
- // calculate shifted left prefix product
- for (int i = 1; i < nums.length; i++) {
- ans[i] = ans[i - 1] * nums[i - 1];
- }
- // calculate shifted right prefix product and answer
- int rightProduct = 1;
- for (int i = nums.length - 2; i >= 0; i--) {
- rightProduct = rightProduct * nums[i + 1];
- ans[i] = ans[i] * rightProduct;
- }
- return ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement