成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 238. Product of Array Except Self

Loong_T / 1816人閱讀

Problem

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

Solution
class Solution {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] res = new int[n];
        res[0] = 1;
        for (int i = 1; i < n; i++) res[i] = res[i-1]*nums[i-1];
        int right = 1;
        for (int i = n-1; i >= 0; i--) {
            res[i] *= right;
            right *= nums[i];
        }
        return res;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65071.html

相關(guān)文章

  • 【Python】LeetCode 238. Product of Array Except Self

    摘要:題目描述題目解析簡(jiǎn)單來(lái)說(shuō)就是對(duì)于數(shù)組中每一項(xiàng),求其他項(xiàng)之積。算一遍全部元素的積再分別除以每一項(xiàng)要仔細(xì)考慮元素為零的情況。沒(méi)有零直接除下去。一個(gè)零零的位置對(duì)應(yīng)值為其他元素之積,其他位置為零。兩個(gè)以上的零全部都是零。 題目描述 Given an array of n integers where n > 1, nums, return an array output such that o...

    kaka 評(píng)論0 收藏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 equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n). For...

    henry14 評(píng)論0 收藏0
  • 238. Product of Array Except Self

    問(wèn)題: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)....

    劉永祥 評(píng)論0 收藏0
  • [LeetCode] Product of Array Except Self

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

    golden_hamster 評(píng)論0 收藏0
  • [Leetcode] Product of Array Except Self 自身以外的數(shù)組乘積

    摘要:動(dòng)態(tài)規(guī)劃復(fù)雜度時(shí)間空間思路分析出自身以外數(shù)組乘積的性質(zhì),它實(shí)際上是自己左邊左右數(shù)的乘積,乘上自己右邊所有數(shù)的乘積。所以我們可以用一個(gè)數(shù)組來(lái)表示第個(gè)數(shù)字前面數(shù)的乘積,這樣。同理,我們可以反向遍歷一遍生成另一個(gè)數(shù)組。 Product of Array Except Self Given an array of n integers where n > 1, nums, return an...

    rockswang 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<