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> productExceptSelf(vector<int>& nums) {
 4         vector<int> ans(nums.size(),1);
 5         if(nums.size()==1)  return ans;
 6         for(int i=1; i<nums.size(); i++)    ans[i]=ans[i-1]*nums[i-1];  //i之前的部分先乘
 7
 8         int sum=1;
 9         for(int i=nums.size()-2; i>=0; i--) //i后面部分用个变量sum存。
10         {
11             sum*=nums[i+1];
12             ans[i]*=sum;
13         }
14         return ans;
15     }
16 };

AC代码

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

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

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

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

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 [