`

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

[分析]
基本思路:数组中第 i 个元素除自身外的数组乘积=product(1,i-1) * product(i + 1, N -1),
因此开辟两个数组prior 和 after,prior[i]=product(1,i-1), after[i]=product(i + 1, N -1)。
优化思路:在基本思路基础上如何优化空间效率呢,能否只使用常数的额外空间?注意到prior[i]=prior[i-1]*nums[i-1],且我们只需要当前的prior[i],因此只使用一个变量即可,每次迭代时更新即可,而原来的after[]就可以直接存放在结果数组中,结果从前往后计算,计算到i位置时,i前面的都是最终结果,i后面的则是基本思路中after[]数组内容。

public class Solution {
    // base version
    public int[] productExceptSelf1(int[] nums) {
        if (nums == null) return null;
        int N = nums.length;
        int[] ret = new int[N];
        int[] prior = new int[N];
        int[] after = new int[N];
        prior[0] = 1;
        for (int i = 1; i < N; i++) {
            prior[i] = prior[i - 1] * nums[i - 1];
        }
        after[N - 1] = 1;
        for (int i = N - 2; i >= 0; i--) {
            after[i] = after[i + 1] * nums[i + 1];
        }
        for (int i = 0; i < N; i++) {
            ret[i] = prior[i] * after[i];
        }
        return ret;
    }
    // optimized version
    public int[] productExceptSelf(int[] nums) {
        if (nums == null) return null;
        int N = nums.length;
        int[] ret = new int[N];
        ret[N - 1] = 1;
        for (int i = N - 2; i >= 0; i--) {
            ret[i] = ret[i + 1] * nums[i + 1];
        }
        int prior = 1;
        for (int i = 0; i < N; i++) {
            ret[i] = prior * ret[i];
            prior *= nums[i];
        }
        return ret;
    }
}
分享到:
评论

相关推荐

    python-leetcode题解之238-Product-of-Array-Except-Self.py

    python python_leetcode题解之238_Product_of_Array_Except_Self.py

    Leetcode 238. Product of Array Except Self

    思路:题目中说不能用除法;用暴力,会超时。 为了计算 除第i个元素的乘积,可以将数组分为三个部分:1…i-1; i; i+1…n (注:这里的i代表序号,不是数组下标)  output[i] = a * b[被乘数a:代表前i-1个数的乘积...

    戳气球leetcode-leetcode:leetcode

    leetcode category other hot keywords:Palindrome(mic), Subsequence Array 螺旋矩阵Spiral Matrix 顺时针打印矩阵 Next Permutation Product of Array Except Self 189.rotate-array 283.move-zero Range Sum ...

    LeetCodeTrain:这是来自LeetCode的已解决任务的存储库

    CoinChange.java - //leetcode.com/problems/coin-change/ ProductOfArrayExceptSelf.java - //leetcode.com/problems/product-of-array-except-self/ LongestRepeatingCharacterReplacement.java - //leetcode....

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...

    LeetCode最全代码

    318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-...

    算法刷题笔记leetcode/lintcode

    - Product of Array Except Self(数组中除了自身以外的乘积) - Partition Array(分割数组) - First Missing Positive(缺失的第一个正数) - 2 Sum(两数之和) - 3 Sum(三数之和) - 3 Sum Closest(三...

    lrucacheleetcode-LeetCode_30Day:力扣30天挑战赛

    lru缓存leetcode 力扣_30天 力扣 30 天挑战赛 日 问题描述 问题和解决方案链接 Git 解决方案页面 1 SINGLE NUMBER 2 HAPPY NUMBER 3 MAXIMUM SUBARRAY 4 Move Zeroes 5 Best Time to Buy and Sell Stock II 6 GROUP ...

    lrucacheleetcode-leetcode:记录自己的leetcode解题历程~Welcomeeveryonetocomment:grinning_face:~

    lru缓存leetcode 我们使用python来处理关于leetcode和linkcode的话题。 leetcode 网址 = 链接代码网址 = 大致分为几个主题: ————————————————————————————————————————...

    Leetcode部分试题解析

    13. **Product of Array Except Self**:不包含自身的数组乘积。可以使用前缀和和后缀乘积的概念,结合位运算优化计算。 14. **Invert Binary Tree**:翻转二叉树。这可以通过递归实现,每个节点的左子树和右子树...

Global site tag (gtag.js) - Google Analytics