leetcode 238. Product of Array Except Self

257 篇文章 17 订阅

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

这道题难点在于不能使用除法,想通了思路就很简单。然而我的解法并没有做到solve it without extra space.

package leetcode;

import java.util.Arrays;

public class Product_of_Array_Except_Self_238 {

	public int[] productExceptSelf(int[] nums) {
		int length=nums.length;
		int[] result=new int[length];
		int[] leftProduct=new int[length];//每个leftProduct[i]里存nums[0]~nums[i-1]的乘积
		int[] rightProduct=new int[length];//每个rightProduct[i]里存nums[i+1]~nums[length-1]的乘积		
		leftProduct[0]=1;
		for(int i=1;i<length;i++){
			leftProduct[i]=leftProduct[i-1]*nums[i-1];
		}
		rightProduct[length-1]=1;
		for(int i=length-2;i>=0;i--){
			rightProduct[i]=rightProduct[i+1]*nums[i+1];
		}
		for(int i=0;i<length;i++){
			result[i]=leftProduct[i]*rightProduct[i];
		}
		return result;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Product_of_Array_Except_Self_238 p=new Product_of_Array_Except_Self_238();
		int[] nums=new int[]{1,2,3,4};
		System.out.println(Arrays.toString(p.productExceptSelf(nums)));
	}

}
有大神跟我想法一样,但是解法上并没有用到extra space.

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;
}
另外一个大神也是差不多的想法:
The idea is simply. The product basically is calculated using the numbers before the current number and the numbers after the current number. Thus, we can scan the array twice. First, we calcuate the running product of the part before the current number. Second, we calculate the running product of the part after the current number through scanning from the end of the array.

public int[] productExceptSelf(int[] nums) {
    int leng = nums.length;
    int[] ret = new int[leng];
    if(leng == 0)
        return ret;
    int runningprefix = 1;
    for(int i = 0; i < leng; i++){
        ret[i] = runningprefix;
        runningprefix*= nums[i];
    }
    int runningsufix = 1;
    for(int i = leng -1; i >= 0; i--){
        ret[i] *= runningsufix;
        runningsufix *= nums[i];
    }
    return ret;  
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值