LeetCode238. Product of arrays other than itself (c++ implementation)

The product of arrays other than itself (c++ implementation)

First look at the topic:

给你一个长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中
除 nums[i] 之外其余各元素的乘积。

示例:

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

提示:题目数据保证数组之中任意元素的全部前缀元素和后缀(甚至是整个数组)的乘积都在 32 位整数范围内。

说明: 请不要使用除法,且在 O(n) 时间复杂度内完成此题。

进阶:
你可以在常数空间复杂度内完成这个题目吗?( 出于对空间复杂度分析的目的,输出数组不被视为额外空间。)

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/product-of-array-except-self

analysis:

First of all, if there is no division restriction, the problem will be much simpler, one traverse to find the product of all the elements of the array, and then another traversal to divide the product by the element, which is the result value. But this will have two problems:

  • 1. If there is an element equal to 0, then the method is invalid
  • 2. If the product of all elements is greater than INT_MAX, the result overflow data will be inaccurate

Realization ideas:

Since the title suggests using the product of prefix and suffix, we can traverse the array and take the product of the prefix element and the product of the suffix of each element, and then the product of the suffix and the suffix is ​​the result, such as
array: arr[0~n] , Arr[1] the product of the array other than itself = (prefix product arr[0]) * (suffix product arr[2]*...arr[n])
The following is the implementation code:

	vector<int> productExceptSelf(vector<int>& nums) {
    
    
        int len = nums.size();
        vector<int > res(len, 1);
        vector<int > left(len, 1);
        vector<int > right(len, 1);
        for (int i = 1; i < len; i++) {
    
    
        	//注:arr[0] 没有前缀乘积默认为1, arr[len-1] 没有后缀乘积, 默认为1
            left[i] = left[i - 1] * nums[i - 1];	//记录每个元素的前缀乘积
            right[len - i - 1] = right[len - i] * nums[len - i];	记录每个元素的后缀乘积
        }
        for (int i = 0; i < len; i++) {
    
    
            res[i] = left[i] * right[i];
        }
        return res;
    }

Optimization:

It can be seen from the code that the time complexity is O(n) and the space complexity is O(n). The method has room for optimization. The space complexity can be optimized to O(1), that is, outside the output array res , Use only constant level space; the
idea is: first use res instead of left to store the prefix product of each element, and then use a constant temp to store the suffix product of the current element, temp * res[i] is arr[i ] The product of arrays other than itself.
The following is the implementation code:

	vector<int> productExceptSelf(vector<int>& nums) {
    
    
        int len = nums.size();
        vector<int > res(len, 1);
        for (int i = 1; i < len; i++) {
    
    
            res[i] = res[i - 1] * nums[i - 1];	//记录每个元素的前缀乘积
        }
        int temp = 1;
        for (int i = len - 2; i >= 0; i--) {
    
    
            temp *= nums[i + 1];	//记录当前元素的后缀乘积
            res[i] = res[i] * temp; //当前元素的前缀乘积 * 当前元素的后缀乘积
        }
        return res;
    }

Guess you like

Origin blog.csdn.net/h799710/article/details/106569682