1. 程式人生 > >LeetCode(238) Product of Array Except Self

LeetCode(238) Product of Array Except Self

一開始最直接的思路當然是:對於0號位置元素,將剩下的元素依次累乘,然後儲存;對於1號位置,重複上述動作。

可是題目的意思是我們只能在o(n)時間內完成,上述演算法的時間的複雜度是o(n^2),下面在已有方案上優化,優化的方法是找出冗餘計算,我們發現:以0號位置和1號位置為例,如果我們已經知道0號位置對應的乘積為p,那麼1號位置對應的乘積為p/nums[1]*nums[0],即0號位置和1號位置對應的乘積是有聯絡的,0號位置的乘積可以-》推出1號位置,同樣1號位置可以推出2號位置等等
至此,演算法已經是可行的。

後來實際編碼時,我發現為何不一開始將所有項相乘,然後再除以對應位置呢,終於找到最佳演算法。

c++程式碼如下

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {

        vector<int> result(nums.size(), 0);

        long long sum = 1;
        //deal with circumtance in which has 0 element
         for(int i = 0; i < nums.size(); i++) {

             if(nums[i] == 0
) { for(int j = 0; j < i; j++) sum *= nums[j]; for(int j = i + 1; j < nums.size(); j++) sum *= nums[j]; result[i] = sum; return result; } } for(int i = 0
; i < nums.size(); i++) sum *= nums[i]; for(int i = 0; i < nums.size(); i++) result[i] = sum / nums[i]; return result; } };

相關推薦

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

leetcode 238 Product of Array Except Self

這個題沒啥知識點,就是思路問題,還是笨啊。。。 題目大意就是返回一個數組,數組裡的每個元素等於原來陣列除了對應的索引的元素之外的所有元素的乘積 Given an array nums of n integers where n &g

LeetCode(238) Product of Array Except Self

一開始最直接的思路當然是:對於0號位置元素,將剩下的元素依次累乘,然後儲存;對於1號位置,重複上述動作。 可是題目的意思是我們只能在o(n)時間內完成,上述演算法的時間的複雜度是o(n^2),下面在已有方案上優化,優化的方法是找出冗餘計算,我們發現:以0號位置

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

leetcode238.Product of Array Except Self

題目描述 給定一個包含n個數字的陣列nums,n>1,返回一個數組output,其中output[i]的內容為除了nums[i]以外的nums中其他所有元素的乘積,要求不使用除號,且時間複雜度為O(n)。 思路 由於不能使用除號,所以必須只有乘號來計算。維

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

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

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

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

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 (後兩種方法有待進一步研究)

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

[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

leetcode Product of Array Except Self

題目要求複雜度為O(n),且不能用除法 那算從左和從右開始乘的成績,然後算到某一位,直接找左邊的乘積和右邊的乘積,乘起來就行 class Solution { public: vector<int> productExceptSelf(vec

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] 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-m-Product of Array Except Self

生成一個數組,每項的值等於除它本身外全陣列所有其它數字的乘積。要求不能用除法,時間複雜度要O(n)。 本來看到不能用除法一下就想到遍歷時每項的值肯定儲存其之前所有項的乘積,但又要求O(n),覺得一次遍歷搞不定啊難道有妙招?於是網搜了下,大多數的解法是左向遍歷每項儲存之前所有

Product of array except self

Product of array except self vector productExceptSelf(vector& nums) { int product = 1; //乘積從1 開始 int zero_product