Product of Array Except Itself

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements ofnums except nums[i].

Solve it without division and in O(n).

For example, given [1,2,3,4], return [24,12,8,6].

Follow up:
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)

Analyse: Use a left vector and right vector to record the product until current number from left / right.  Space O(n), time O(n).

 1 class Solution {
 2 public:
 3     vector<int> productExceptSelf(vector<int>& nums) {
 4         vector<int> left(nums.size(), 1);
 5         vector<int> right(nums.size(), 1);
 6
 7         for(int i = 1; i < nums.size(); i++)
 8             left[i] = left[i - 1] * nums[i - 1];
 9         for(int i = nums.size() - 2; i >= 0; i--)
10             right[i] = right[i + 1] * nums[i + 1];
11
12         vector<int> result(nums.size(), 1);
13         for(int i = 0; i < nums.size(); i++)
14             result[i] = left[i] * right[i];
15
16         return result;
17     }
18 };

Use the result to store left production and a variable to store the right production. Space O(1), time O(n)

 1 class Solution {
 2 public:
 3     vector<int> productExceptSelf(vector<int>& nums) {
 4         int n = nums.size();
 5         vector<int> result(n, 1);
 6
 7         // first scan nums, and store its left production in result
 8         for(int i = 1; i < n; i++)
 9             result[i] = result[i - 1] * nums[i - 1];
10
11         int right = nums[n - 1];
12         for(int i = n - 2; i >= 0; i--) {
13             result[i] *= right;
14             right *= nums[i];
15         }
16         return result;
17     }
18 };
时间: 2024-10-21 18:07:27

Product of Array Except Itself的相关文章

【08_238】Product of Array Except Self

Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of numsexcept nums[i

【LeetCode】Product of Array Except Self

Product of Array Except Self Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2

[LintCode] Product of Array Except Self 除本身之外的数组之积

Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WITHOUT divide operation. Have you met this question in a real interview? Yes Example For A = [1, 2, 3], return [6, 3, 2]. LeetCode上的原题,请参见我之前的博客Product

#leetcode#Product of Array Except Self

Product of Array Except Self Total Accepted: 442 Total Submissions: 1138My Submissions Question  Solution Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums e

LeetCode Q238 Product of Array Except Self(Medium)

Product of Array Except Self 原题: Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n). For example, given [

Leetcode 238. Product of Array Except Self

238. Product of Array Except Self Total Accepted: 51070           Total Submissions: 116543           Difficulty: Medium Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the ele

[leedcode 238] Product of Array Except Self

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2,3,4], return [24,12,8,6]. Fo

LeetCode OJ 238. Product of Array Except Self 解题报告

题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Self My Submissions Question Total Accepted: 36393 Total Submissions: 87262 Difficulty: Medium Given an array of n integers where n > 1, nums, return an arr

leetcode:238. Product of Array Except Self(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and i

【LeetCode】238. Product of Array Except Self

Product of Array Except Self Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2