leetcode238-product of arrays other than itself

Problem Description

Given an integer array nums of length n, where n> 1, returns the output array output, where output[i] is equal to the product of all the elements in nums except nums[i].

Example:

输入: [1,2,3,4]
输出: [24,12,8,6]

Tip: The title data guarantees that the product of all prefix elements and suffixes (even the entire array) of any element in the array is within the range of 32-bit integers.

Explanation: Please do not use division, and complete this question in O(n) time complexity.

Advanced:
Can you complete this problem within a constant space complexity? (For the purpose of space complexity analysis, the output array is not considered as extra space.)

Problem-solving idea 1: (List of left and right products)

Instead of dividing the product of all numbers by the number at a given index to get the corresponding answer, we use the product of all the numbers on the left of the index and the product of all the numbers on the right (ie prefix and suffix) to get the answer.

For a given index i, we will multiply the product of all the numbers on the left by the product of all the numbers on the right. Let's take a look at the code below

class Solution {
    
    
    public int[] productExceptSelf(int[] nums) {
    
    
        int n=nums.length;
        if(nums.length<=1){
    
    
            return nums;
        }
        int [] pre=new int[n];      //pre表示从某个元素左边所有元素的乘积
        int [] next=new int[n];     //next表示某个元素右边的所有元素的乘积
        pre[0]=1;           //初始最左边的数的乘积
        for(int i=1;i<n;i++){
    
       //依次计算左边所有元素的乘积放入到数组中
            pre[i]=pre[i-1]*nums[i-1];
        }
        next[n-1]=1;
        for(int i=n-2;i>=0;i--){
    
        //依次计算右边所有元素的成绩放入到数组中
            next[i]=next[i+1]*nums[i+1];
        }
        int []res=new int[n];
        res[0]=next[0];
        res[n-1]=pre[n-1];
        //左边所有元素的乘积乘以右边所有元素的乘积
        for(int i=1;i<n-1;i++){
    
    
            res[i]=pre[i]*next[i];
        }
        return res;
    }
}

Problem-solving idea 2: Space complexity O(1)

Although the above method has been able to solve this problem well, the space complexity is not constant.

Since the output array is not included in the space complexity, we can use the output array to calculate the L or R array. First calculate the output array as the L array, and then use left to dynamically save the result of the product of all elements on the left side of the nums array. Let's take a look at the algorithm based on this idea.

class Solution {
    
    
    public int[] productExceptSelf(int[] nums) {
    
    
        int n=nums.length;
        if(nums.length<=1){
    
    
            return nums;
        }
        //res数组不仅起到保存结果值的作用,也起到了保存右边所有元素乘积的作用
        int res[]=new int[n];
        //先让res数组保存右边所有元素的乘积
        res[n-1]=1;
        for(int i=n-2;i>=0;i--){
    
    
            res[i]=res[i+1]*nums[i+1];
        }
        int left=1;     //left可以用来累积左边元素的乘积
        //再依次计算每个元素的其余元素的乘积,而res[0]已经计算出来了
        for(int i=1;i<n;i++){
    
    
            left*=nums[i-1];
            res[i]=left*res[i];
        }
        return res;
    }
}

Guess you like

Origin blog.csdn.net/qq_39736597/article/details/114285557