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 ofnums
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
題目大意:某一整型陣列,對於其中的每個元素,計算陣列中除了該元素之外所有元素的積,用陣列返回結果。
題目思路:
解法一:先對於每一個元素,計算這個元素左邊所有元素的積,再用該乘積乘上該元素右邊所有元素的乘積。重點是採取累乘的作法,一共只遍歷陣列兩遍,避免O(n*n)的時間複雜度。程式碼如下:(2ms,beats 37.78%)
public int[] productExceptSelf(int[] nums) { int len = nums.length,rightProduct=1; int[] output = new int[len]; output[0] = 1; for (int i = 1; i < len; i++) { output[i] = output[i - 1] * nums[i - 1]; } for (int i = len - 1; i >= 0; i--) { output[i] *= rightProduct; rightProduct *= nums[i]; } return output; }
解法二:計算所有元素的乘積 product,則對於每一個元素nums[i],當nums[i] 不為零時,除該元素外所有元素的乘積output[I]=product/nums[i]。若nums[i] = 0,則所有j != i,output[j] = 0,再遍歷一遍陣列計算output[i]即可。程式碼如下:(2ms,beats 37.78%)
public int[] productExceptSelf(int[] nums) { int len = nums.length, product = 1; int[] output = new int[len]; for (int num : nums) product *= num; for (int i = 0; i < len; i++) { if (nums[i] == 0) { int k = 1; for (int j = 0; j < i && k != 0; j++) k *= nums[j]; for (int j = i + 1; j < len && k != 0; j++) { k *= nums[j]; output[j] = 0; } output[i] = k; return output; } output[i] = product / nums[i]; } return output; }
相關推薦
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
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 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 238 product of array except self 除自身以外陣列的乘積
題目連結 https://leetcode-cn.com/problems/product-of-array-except-self/ 題意 中文題,就是給出一個數組,輸出也是一個數組,每個位置是除自身外其他所有數的乘積。要求不
[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
【leetcode】238.Product of Array Except Self
題目描述 給定一個包含n個數字的陣列nums,n>1,返回一個數組output,其中output[i]的內容為除了nums[i]以外的nums中其他所有元素的乘積,要求不使用除號,且時間複雜度為O(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
LeetCode(238) Product of Array Except Self
一開始最直接的思路當然是:對於0號位置元素,將剩下的元素依次累乘,然後儲存;對於1號位置,重複上述動作。 可是題目的意思是我們只能在o(n)時間內完成,上述演算法的時間的複雜度是o(n^2),下面在已有方案上優化,優化的方法是找出冗餘計算,我們發現:以0號位置
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 el
3.2.1 LeetCode陣列類題目選做(1)—— First Missing Positive & Majority Element & Product of Array Except Self
陣列題目概述 陣列的題目很多很重要,一般和其他知識點綜合應用。包括Two pointer,Binary Search,Dynamic Programming,Greedy,Backtracking 等,各類演算法都將分別選做一些題目學習交流總結。 這一系列選擇出一些非應用
238-m-Product of Array Except Self
生成一個數組,每項的值等於除它本身外全陣列所有其它數字的乘積。要求不能用除法,時間複雜度要O(n)。 本來看到不能用除法一下就想到遍歷時每項的值肯定儲存其之前所有項的乘積,但又要求O(n),覺得一次遍歷搞不定啊難道有妙招?於是網搜了下,大多數的解法是左向遍歷每項儲存之前所有
[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
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
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