Given an integer array nums
, return an array answer
such that answer[i]
is equal to the product of all the elements of nums
except nums[i]
.
The product of any prefix or suffix of nums
is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n)
time and without using the division operation.
Example 1:
Input: nums = [1,2,3,4] Output: [24,12,8,6]
Example 2:
Input: nums = [-1,1,0,-3,3] Output: [0,0,9,0,0]
Constraints:
2 <= nums.length <= 105
-30 <= nums[i] <= 30
- The product of any prefix or suffix of
nums
is guaranteed to fit in a 32-bit integer.
Follow up: Can you solve the problem in O(1)
extra space complexity? (The output array does not count as extra space for space complexity analysis.)
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = [1] * n
left = right = 1
for i in range(n):
ans[i] = left
left *= nums[i]
for i in range(n - 1, -1, -1):
ans[i] *= right
right *= nums[i]
return ans
class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] ans = new int[n];
for (int i = 0, left = 1; i < n; ++i) {
ans[i] = left;
left *= nums[i];
}
for (int i = n - 1, right = 1; i >= 0; --i) {
ans[i] *= right;
right *= nums[i];
}
return ans;
}
}
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function (nums) {
const n = nums.length;
let ans = new Array(n);
for (let i = 0, left = 1; i < n; ++i) {
ans[i] = left;
left *= nums[i];
}
for (let i = n - 1, right = 1; i >= 0; --i) {
ans[i] *= right;
right *= nums[i];
}
return ans;
};
function productExceptSelf(nums: number[]): number[] {
const n = nums.length;
let ans = new Array(n);
for (let i = 0, left = 1; i < n; ++i) {
ans[i] = left;
left *= nums[i];
}
for (let i = n - 1, right = 1; i >= 0; --i) {
ans[i] *= right;
right *= nums[i];
}
return ans;
}
function productExceptSelf(nums: number[]): number[] {
return nums.map((_, i) =>
nums.reduce((pre, val, j) => pre * (i === j ? 1 : val), 1),
);
}
func productExceptSelf(nums []int) []int {
n := len(nums)
ans := make([]int, n)
left, right := 1, 1
for i := 0; i < n; i++ {
ans[i] = left
left *= nums[i]
}
for i := n - 1; i >= 0; i-- {
ans[i] *= right
right *= nums[i]
}
return ans
}
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n = nums.size();
vector<int> ans(n);
for (int i = 0, left = 1; i < n; ++i) {
ans[i] = left;
left *= nums[i];
}
for (int i = n - 1, right = 1; i >= 0; --i) {
ans[i] *= right;
right *= nums[i];
}
return ans;
}
};
impl Solution {
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
let mut dp_left = vec![1_i32; nums.len()];
let mut dp_right = vec![1_i32; nums.len()];
for i in 1..nums.len() {
dp_left[i] = dp_left[i - 1] * nums[i - 1];
}
for i in (0..(nums.len() - 1)).rev() {
dp_right[i] = dp_right[i + 1] * nums[i + 1];
}
dp_left
.into_iter()
.enumerate()
.map(|(i, x)| x * dp_right[i])
.collect()
}
}
impl Solution {
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
let n = nums.len();
let mut l = 1;
let mut r = 1;
let mut res = vec![0; n];
for i in 0..n {
res[i] = l;
l *= nums[i];
}
for i in (0..n).rev() {
res[i] *= r;
r *= nums[i];
}
res
}
}