【Leetcode】:238. Product of Array Except Self 问题 in JAVA

来源:互联网 发布:淘宝客服基础培训知识 编辑:程序博客网 时间:2024/06/17 13:23

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

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

题目要求返回一个数组,第i个位置的值是在输入数组中,除了i位置的数的所有数的乘积。

但是要求不能用除法!



用除法解决这个问题很简单只需要先把所有数连乘,生成结果的时候再逐一除以一个数

唯一需要考虑的地方就是数组中的0,

情况一:只要数组中的0的数量大于等于2,那么返回的值一定全为0。

情况二:如果数组中只有一个0,那么返回值除了i位置的数,其他返回值一定也为0。

情况三:输入数组中没有0,简单操作


public class Solution {    public int[] productExceptSelf(int[] nums) {        int product = 1;        int zeros = 0;//记录数组中0的个数        int[] result = new int[nums.length];        for (int num : nums) {            if (num==0) {                zeros++;                if (zeros == 2) { //当数组中有2个以上0时,返回的结果一定全是0                    return result;                }            } else {                product = product * num;            }        }        for (int i = 0; i < nums.length; i++) {            if (zeros == 0) {//数组中没有0的情况                result[i] = product / nums[i];               } else {//数组中有一个0的情况                if (nums[i] == 0) {                    result[i] = product;                }            }        }        return result;    }}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

下面是不用除法的方案:

需要进行两次for循环,第一次循环中把result[i]赋值为前面的数nums的前i-1个数的乘积

第二次for循环把temp赋值为num的后i-1个数的乘积

public class Solution {    public int[] productExceptSelf(int[] 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 - 2; i >= 0; i--) {            temp = temp * nums[i + 1];            result[i] = result[i] * temp;        }        return result;    }}


0 0
原创粉丝点击