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


要求

题目比较好理解,但是有几个关键点这里需要明确一下:

  1. 不能用除法。意思就是:你不能上来先把所有数乘积算出来,然后再逐个除以每个元素,这种思路是无聊、没技术含量而且不被允许的。
  2. 时间复杂度必须控制到O(n)。意思是:如果用O(n^2)的方法,那外层一个for循环,内层左右遍历就解决了,也是很无聊的解法。
  3. 空间复杂度最好是常数,但是重新分配的返回数组不算在内。

思路1

我们以一个4个元素的数组为例,nums=[a1, a2, a3, a4]。

想在O(n)时间复杂度完成最终的数组输出,res=[a2*a3*a4, a1*a3*a4, a1*a2*a4, a2*a3*a4]。

比较好的解决方法是构造两个数组相乘:

  1. [1, a1, a1*a2, a1*a2*a3]
  2. [a2*a3*a4, a3*a4, a4, 1]

这样思路是不是清楚了很多,而且这两个数组我们是比较好构造的。

AC代码如下:

    public int[] productExceptSelf(int[] nums) {
        int len = nums.length;
        int[] pSeq = new int[nums.length];
        int[] nSeq = new int[nums.length];

        pSeq[0] = 1;
        for (int i = 1; i < len; i ++) {
            pSeq[i] = pSeq[i - 1] * nums[i - 1];
        }

        nSeq[len - 1] = 1;
        for (int i = len - 2; i >= 0; i --) {
            nSeq[i] = nSeq[i + 1] * nums[i + 1];
        }

        for (int i = 0; i < len; i ++) {
            pSeq[i] *= nSeq[i];
        }

        return pSeq;
    }

但是,上面的空间复杂度为O(N),不满足常数时间复杂度。我们可以对上面的代码进行空间优化,用一个常数p来保存每次计算的结果值。

优化AC代码:

        int len = nums.length, p;
        int[] arr = new int[nums.length];

        arr[0] = p = 1;
        for (int i = 1; i < len; i ++) {
            p = p * nums[i - 1];
            arr[i] = p;
        }

        p = 1;
        for (int i = len - 2; i >= 0; i --) {
            p = p * nums[i + 1];
            arr[i] *= p;
        }

        return arr;

思路2

本以为这样就已经很不错了,但是在discuss讨论区发现了一个特别牛逼的递归解法,非常精妙,这里分享给大家。

    public int[] productExceptSelfRev(int[] nums) {
        multiply(nums, 1, 0, nums.length);

        return nums;
    }

    private int multiply(int[] a, int fwdProduct, int indx, int N) {
        int revProduct = 1;
        if (indx < N) {
            revProduct = multiply(a, fwdProduct * a[indx], indx + 1, N);
            int cur = a[indx];
            a[indx] = fwdProduct * revProduct;
            revProduct *= cur;
        }
        return revProduct;
    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-12 19:20:46

[LeetCode]Product of Array Except Self,解题报告的相关文章

LeetCode OJ 238. Product of Array Except Self 解题报告

题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Self My Submissions Question Total Accepted: 36393 Total Submissions: 87262 Difficulty: Medium Given an array of n integers where n > 1, nums, return an arr

#leetcode#Product of Array Except Self

Product of Array Except Self Total Accepted: 442 Total Submissions: 1138My Submissions Question  Solution 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 e

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

LeetCode -- Product of Array Except Self My Submissions Question

Question: 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,1

LeetCode Intersection of Two Linked Lists 解题报告

https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 求两个链表的第一个公共节点,如果不存在公共节点的话就返回null. A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 解题思路: 1)如果两个链表的最后一个节点一样,那么说明两个链表一定有交点. 2)分别求出两个链表的长度,然后对长度长的链表向前移动:LengA - LengB,将两个链表进行对齐,之后一起遍历,直到找到第一个相同的节

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

Leetcode 68. Text Justification 文本调整 解题报告

1 解题思想 这道题,其实我也想不通为什么要标记为Hard模式,题目的大意就是对一个字符串数组进行格式化调整,输出对应的句子. 要求有: 1.每一行的字符串长度不能超过一个固定长度maxWidth 2.每两个单词之间必须有一个空格,如果一行之间的单词之间空格不能细分,那么必须左边的空格多,右边的少.并且,空格多的地方只比右边少的多一个 3.最后一行不适用2的空格方式,正常的每个单词空一格就好,最后留白就好 提前贴个解释: * 这道题关键在于仔细的处理每一个步骤: * 1.每一行选择K的单词,K个

238. [LeetCode] Product of Array Except Self

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 elements of nums except nums[i]. Example: Input: [1,2,3,4] Output: [24,12,8,6] Note: Please solve it without division and in

[LeetCode]Binary Search Tree Iterator,解题报告

题目 LeetCode题目如下: mplement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1