[LeetCode]: 238: Product of Array Except Self

來源:互聯網
上載者:User

標籤:

題目:

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 count as extra space for the purpose of space complexity analysis.)

 

思路:

1. 計算所有數相乘的結果,並記錄數組中"0"的個數

2. 再次逐一便利初始給定的數組,根據其中"0"的個數和出現的位置做處理

3. 當只有一個0的時候,結果為除掉這個0的剩下的數的乘積

4. 當有多個0的時候,結果為全零的數組

 

代碼:

public static int[] productExceptSelf(int[] nums) {        int[] arrResult  = new int[nums.length];        int iTemp =1;        int i0Counter=0;                for(int i =0;i < nums.length;i++){            if(nums[i]!= 0){                iTemp= iTemp * nums[i];            }            else{                i0Counter++;            }                    }                if(i0Counter ==0 ){            for(int i =0;i < nums.length;i++){                arrResult[i] = iTemp/nums[i];                            }            return arrResult;        }                else if(i0Counter ==1 ){            for(int i =0;i < nums.length;i++){                if(nums[i] == 0){                    arrResult[i] = iTemp;                }                else{                    arrResult[i] = 0;                }            }            return arrResult;        }                else{            for(int i =0;i < nums.length;i++){                nums[i] =0;            }            return arrResult;        }    }

分析:這個解法時間複雜度超了,使用了額外的空間,並且使用了除法

 

思路2:遍曆累乘的時候,“除去”“當前位置”的元素

1. 先計算當前位置左側元素的乘積,記錄在輸出數組的當前位置上,這樣不使用額外的儲存空間

2. 再從右邊遍曆,記錄當前位置右側的所有元素的乘積,記錄在一個額外的位置上

3. 將兩個數組再逐一相乘

 

代碼如下:

    public static int[] productExceptSelf(int[] nums) {        int[] arrResult  = new int[nums.length];        int[] arrRight   = new int[nums.length];                arrResult[0] = 1;        for(int i = 1;i<nums.length; i++ ){            arrResult[i] = arrResult[i-1]*nums[i-1] ;        }                arrRight[nums.length-1] = 1;        for(int i = nums.length-2; i>= 0; i-- ){            arrRight[i] = arrRight[i+1]*nums[i+1] ;        }                for(int i = 0;i<nums.length; i++ ){            arrResult[i] = arrResult[i]*arrRight[i] ;        }                return arrResult;    }

 

[LeetCode]: 238: Product of Array Except Self

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.