LeetCode 238. Product of Array Except Self

本文探讨了给定数组 nums 中每个元素与其余下元素的乘积问题,提供了无除法和常量空间复杂度的高效算法。首先介绍了使用除法的方法,考虑了数组中0的特殊情况,然后转向不使用除法的优化方法,通过划分数组实现。
摘要由CSDN通过智能技术生成

题目:

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]

Constraint: It's guaranteed that the product of the elements of any prefix or suffix of the array (including the whole array) fits in a 32 bit integer.

Note: Please solve it without division and in O(n).

Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)


先写了个用除法的版本,其实还是有些corner case要注意的,比如如果数组中出现了0。出现0还分两种情况,如果只出现了一次,那只有在它那里的result不是0,如果超过一次,那就全是0了。

Runtime: 1 ms, faster than 99.99% of Java online submissions for Product of Array Except Self.

Memory Usage: 47.3 MB, less than 96.15% of Java online submissions for Product of Array Except Self.

class Solution {
    public int[] productExceptSelf(int[] nums) {
        int all = 1;
        int zeroCount = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                zeroCount++;
            } else {
                all *= nums[i];
            }
        }
        
        int[] result = new int[nums.length];
        if (zeroCount > 1) {
            return result;
        }
        for (int i = 0; i < nums.length; i++) {
            if (zeroCount == 0) {
                result[i] = all / nums[i];
            } else {
                if (nums[i] == 0) {
                    result[i] = all;
                } else {
                    result[i] = 0;
                }
            }
        }
        return result;
    }
}

然后是不用除法的做法。我们可以把整个数组分成两半,left,nums[i],和right。分别求出左半边和右半边乘积,最后相乘就是了。具体的左半边和右半边可以用数组表示,left[i]表示从[0, i - 1]的乘积,right[i]表示[i + 1, len - 1]的乘积。写代码的时候需要注意下标,首先要将left[0]和right[len - 1]初始化成1,然后再for循环里更新left[i]和right[len - i - 1],注意left[i]的乘积是要left[i - 1]乘i - 1,right也同理。

Runtime: 2 ms, faster than 32.44% of Java online submissions for Product of Array Except Self.

Memory Usage: 47.7 MB, less than 80.91% of Java online submissions for Product of Array Except Self.

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值