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
class Solution { public: //基本思想是兩個陣列left,right,分別儲存從左到右 從右到左的乘積,然後對應位置相乘即可 //為了優化空間 左邊的陣列可以用一個變數代替 右邊的陣列可以複用結果陣列 vector<int> productExceptSelf(vector<int>& nums) { int len=nums.size(); vector<int> res(len,0); if(len<2) return res; int i; res[len-1]=1;//右邊的乘積 for(i=len-1;i>0;i--) res[i-1]=res[i]*nums[i]; int left=1;//左邊的乘積 for(i=0;i<len;i++) { res[i]*=left; left=left*nums[i]; } return res; } };
相關推薦
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
LeetCode 238 product of array except self 除自身以外陣列的乘積
題目連結 https://leetcode-cn.com/problems/product-of-array-except-self/ 題意 中文題,就是給出一個數組,輸出也是一個數組,每個位置是除自身外其他所有數的乘積。要求不
[Swift]LeetCode238. 除自身以外陣列的乘積 | Product of Array Except Self
Given an array nums of n integers where n > 1, return an array outputsuch that output[i] is equal to
[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
3.2.1 LeetCode陣列類題目選做(1)—— First Missing Positive & Majority Element & Product of Array Except Self
陣列題目概述 陣列的題目很多很重要,一般和其他知識點綜合應用。包括Two pointer,Binary Search,Dynamic Programming,Greedy,Backtracking 等,各類演算法都將分別選做一些題目學習交流總結。 這一系列選擇出一些非應用
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 eleme
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
238. Product of Array Except Self
return value amp ron clas public cnblogs nts and Problem statement: Given an array of n integers where n > 1, nums, return an array o
[Leetcode]238. Product of Array Except Self
output all exc without pro integer put num rod Given an array of n integers where n > 1, nums, return an array output such that output
[leetcode] Product of Array Except Self
rpo lee 般的 兩個 cep array int nbsp and Given an array nums of n integers where n > 1, return an array output such that output[i] is e
238. Product of Array Except Self(python+cpp)
題目: 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 elemen
[LeetCode] 238 Product of Array Except Self
Given an array nums of n integers where n > 1, return an array output such that output[i] 
238. Product of Array Except Self - Medium
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ
Product of array except self
Product of array except self vector productExceptSelf(vector& nums) { int product = 1; //乘積從1 開始 int zero_product
leetcode Product of Array Except Self
題目要求複雜度為O(n),且不能用除法 那算從左和從右開始乘的成績,然後算到某一位,直接找左邊的乘積和右邊的乘積,乘起來就行 class Solution { public: vector<int> productExceptSelf(vec
【leetcode】238.Product of Array Except Self
題目描述 給定一個包含n個數字的陣列nums,n>1,返回一個數組output,其中output[i]的內容為除了nums[i]以外的nums中其他所有元素的乘積,要求不使用除號,且時間複雜度為O(n)。 思路 由於不能使用除號,所以必須只有乘號來計算。維
Facebook面試題專題3 - leetcode238. Product of Array Except Self/56. Merge Intervals
238. Product of Array Except Self 題目描述 給定一個n個元素的陣列nums(n > 1),返回陣列output。其中output[i] 等於除了元素nums[i]的其餘元素的乘積。 要求:不要分治,時間複雜度O(n
LeetCode刷題MEDIM篇Product of Array Except Self
題目 Given an array nums of n integers where n > 1, return an array output such that output[i]&n
leetcode 238 Product of Array Except Self
這個題沒啥知識點,就是思路問題,還是笨啊。。。 題目大意就是返回一個數組,數組裡的每個元素等於原來陣列除了對應的索引的元素之外的所有元素的乘積 Given an array nums of n integers where n &g
238. Product of Array Except Self leetcode java
題目: 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 e