Product of Array Except Self

求除了自己之外数组所有元素的乘积。

新建一个数组,每个位置保存前面所有数的乘积。

反过来,另一个数组每个位置保存后面所有数乘积。

两个数组每个位置相乘即为所求。这样时间复杂度为O(n);

为了保证O(1)space,所以上面的操作进行融合,具体见代码。

 1 class Solution {
 2 public:
 3     vector<int> productExceptSelf(vector<int>& nums) {
 4         //思路 顺序求前面所有数的乘机,再反过来,
 5         int n = nums.size();
 6         vector<int> v;
 7         int tmp=1;
 8         v.push_back(1);
 9         for(int i=1;i<n;i++)
10         {
11             tmp *= nums[i-1];
12             v.push_back(tmp);
13         }
14         tmp = nums[n-1];
15         for(int j=n-2;j>=0;j--)
16         {
17             v[j] *= tmp;
18             tmp *= nums[j];
19         }
20
21         return v;
22     }
23 };
时间: 2024-12-11 17:58:12

Product of Array Except Self的相关文章

【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