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 O(n).

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



注释:用两个指针,前面的指针fromBegin比返回的res少乘一个当前的i,后面的指针也是如此。当i到达结尾时,前后都覆盖到了。


#include <cstdio>
#include <vector>
#include<iostream>
using namespace std;

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        int n=nums.size();
        int fromBegin=1;
        int fromLast=1;
        vector<int> res(n,1);

        for(int i=0;i<n;i++){
            res[i]*=fromBegin;
            fromBegin*=nums[i];
            res[n-1-i]*=fromLast;
            fromLast*=nums[n-1-i];
        }
        return res;
    }
};
int main(){
    vector<int> temp;
    temp.push_back(2);
    temp.push_back(2);
    temp.push_back(2);
    temp.push_back(2);
    temp.push_back(2);
    temp.push_back(2);
    temp.push_back(2);
    temp.push_back(2);
    Solution test;
    test.productExceptSelf(temp);
    return 0;
} 

原文地址:https://www.cnblogs.com/250101249-sxy/p/10438014.html

时间: 2024-10-03 02:59:25

238. [LeetCode] Product of Array Except Self的相关文章

#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 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 除本身之外的数组之积

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

[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 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 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

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