[LC][Array]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 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[i], 求的是原数组nums中除了的nums[i]的其它数字的乘积。

  • 于是,第一遍遍历得到nums中所有元素乘积productAll;
  • 第二遍遍历时将元素更新为productAll/nums[i]即可。


然后突然想到0这个捣蛋鬼~它会影响productAll,也会使分母为0。所以怎么办呢?

  • 如果数组里只有一个nums[i] = 0,那么output数组里,除了nums[i]是其它非零元素乘积,其他output的元素全是0!
  • 如果数组里有大于一个0,那么output数组里每个元素都为0,因为除了自己的其他元素里必包含至少一个0.

所以,还要用一个变量记录0元素个数,一个变量记录0元素的位置。

class Solution {
    public int[] productExceptSelf(int[] nums) {
        int[] result = new int[nums.length];
            
        int productAll = 1;
        int zeroNum = 0;
        int zeroIdx = -1;
        for(int i = 0; i < nums.length; i ++){
            if(nums[i] == 0){
                zeroNum ++;
                zeroIdx = i;
            }
            else{
                productAll *= nums[i];
            }
            
        }
        
        if(zeroNum > 1){
        }
        else if(zeroNum == 1){
            for(int i = 0; i < nums.length; i ++){
                if(i != zeroIdx){
                    result[i] = 0;
                }
                else{
                    result[i] = productAll;
                }
            }
        }
        else{
            for(int i = 0; i < nums.length; i ++){
                result[i] = productAll / nums[i];
            }
        }
        
        return result;
    }
}
我的实现方法又开辟了一个数组result,其实完全没必要,直接在nums操作就好~

三、淫奇技巧

public 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;
}
搬运解释来自:http://blog.csdn.net/wzy_1988/article/details/46916179

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

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

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

2. 递归

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;
    }


四、举一反三

后面再追加


五、碎碎念

第一次写出无bug代码。。继续加油。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值