Product of Array Except Self

来源:互联网 发布:libgcc s.so.1 ubuntu 编辑:程序博客网 时间:2024/06/14 07:57

题目地址:https://leetcode.com/problems/product-of-array-except-self/#/description

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

如何忽略题目中不准用除法,在O(n)时间内搞定问题的条件话,那这个问题就很好解决。

如果可以用除法,那么先求出数组中所有元素的积:

pro=a1a2a3...an

那么结果为:
res={proa1proa2proa3...proan}

这么做的时间复杂度为O(n),但是用了除法。代码实现如下:

public class ProductofArrayExceptSelf {    public static int[] productExceptSelf(int[] nums) {        if (nums == null || nums.length <= 1)            return nums;        int temp = 1;        for (int i = 0; i < nums.length; i++) {            temp *= nums[i];        }        for (int i = 0; i < nums.length; i++) {            if (nums[i] != 0)                nums[i] = temp / nums[i];            else                nums[i] = 0;        }        return nums;    }}

如果不用除法,时间复杂度先不要保证O(n)可以这么做。

先将结果集初始化:

res={1,1,1,...,1}

其中res的长度与nums的长度相等,然后把nums中的元素依次与res中的元素做乘法,遇到下标相同的则略过,于是这样写代码的时间复杂度为O(n2)。代码实现如下:

public class ProductofArrayExceptSelf {    public static int[] productExceptSelfII(int[] nums) {        if (nums == null || nums.length <= 1)            return nums;        int[] result = new int[nums.length];        for (int i = 0; i < nums.length; i++) {            result[i] = 1;        }        for (int i = 0; i < nums.length; i++) {            for (int j = 0; j < nums.length; j++) {                if (i == j)                    continue;                else                    result[j] *= nums[i];            }        }        return result;    }}

现在题目的要求是既要没有除法,又要O(n),那我们可以构建这么一个数组:

res={r1,r2,r3,...,rn}
r1=1
r2=a1
r3=a1a2
r4=a1a2a3
......
rn1=a1a2a3...an2
rn=a1a2a3...an1

这个数组还是比较好构建的,而且构建这个数组的时间复杂度为O(n),现在瞄一眼这个数组,只有最后一个元素与我们最终的答案是相同的,倒数第二个元素还差个an,倒数第三个元素还差an1an,倒数第四个元素还差an2an1an,……是不是已经看出点什么来了?

对,我们重新迭代一次nums,从后到前再乘一次把缺少的补乘上不就可以了么。这个搞就是做了两次O(n)的操作,但是最终的时间复杂度还是O(n),代码实现如下:

public class ProductofArrayExceptSelf {    public static int[] productExceptSelfIII(int[] nums) {        if (nums == null || nums.length <= 1)            return nums;        int[] result = new int[nums.length];        result[0] = 1;        for (int i = 1; i < nums.length; i++) {            result[i] = result[i - 1] * nums[i - 1];        }        int temp = 1;        for (int i = nums.length - 1; i >= 0; i--) {            result[i] *= temp;            temp *= nums[i];        }        return result;    }    public static void main(String[] args) {        int[] nums1 = {1,2,3,4};        int[] r1 = productExceptSelf(nums1);        int[] nums2 = {1,2,3,4};        int[] r2 = productExceptSelfII(nums2);        int[] nums3 = {1,2,3,4};        int[] r3 = productExceptSelfIII(nums3);        return ;    }}
原创粉丝点击