[LeetCode] 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].

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.)

这道题给定我们一个数组,让我们返回一个新数组,对于每一个位置上的数是其他位置上数的乘积,并且限定了时间复杂度O(n),并且不让我们用除法。如果让用除法的话,那这道题就应该属于Easy,因为可以先遍历一遍数组求出所有数字之积,然后除以对应位置的上的数字。但是这道题禁止我们使用除法,那么我们只能另辟蹊径。我们可以先遍历一遍数组,每一个位置上存之前所有数字的乘积。那么一遍下来,最后一个位置上的数字是之前所有数字之积,是符合题目要求的,只是前面所有的数还需要在继续乘。我们这时候再从后往前扫描,每个位置上的数在乘以后面所有数字之积,对于最后一个位置来说,由于后面没有数字了,所以乘以1就行。参见代码如下:

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        vector<int> res(nums.size(), 1);
        for (int i = 1; i < nums.size(); ++i) {
            res[i] = res[i - 1] * nums[i - 1];
        }
        int right = 1;
        for (int i = nums.size() - 1; i >= 0; --i) {
            res[i] *= right;
            right *= nums[i];
        }
        return res;
    }
};

LeetCode All in One 题目讲解汇总(持续更新中...)

时间: 2024-08-01 21:04:10

[LeetCode] Product of Array Except Self 除本身之外的数组之积的相关文章

[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 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 Product of Array Except Self (除自身外序列之积)

题意:给一个序列nums,要求返回一个序列ans,两序列元素个数相同,ans第i个元素就是除了nums[i]之外所有的数相乘之积. 思路:时间O(n),额外空间O(0). 第一次扫一遍,处理nums[0~i-1]的积作为ans[i],这样的ans[i]就得到了i之前所有数之积,那么只剩下i后面所有数之积. 第二次从后往前扫,跟第一次的做法一样,但是这次得开个临时变量存储积了. 1 class Solution { 2 public: 3 vector<int> productExceptSel

#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——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].

LeetCode 238 Product of Array Except Self(除自身外数组其余数的乘积)

翻译 给定一个有n个数字的数组nums,其中n大于1,返回一个数组使得output[i]等于除nums[i]外所有元素的乘积. 不用分治并且在O(n)复杂度下解决这个问题. 例如,给定[1, 2, 3, 4],返回[24, 12, 8, 6]. 跟进: 你可以只在常量空间下完成它吗? (备注:在空间复杂度计算时输出数组不作为额外空间.) 原文 Given an array of n integers where n > 1, nums, return an array output such t

LeetCode -- Product of Array Except Self My Submissions Question

Question: 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,1

[LeetCode]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].

238. [LeetCode] Product of Array Except Self

Given an array nums of n integers where n > 1,  return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Example: Input: [1,2,3,4] Output: [24,12,8,6] Note: Please solve it without division and in