I. Title Description:

You are given an array of integers of length n, nums, where n > 1 returns output, where output[I] is equal to the product of all elements in nums except nums[I]. Example:

Input: [1,2,3,4] output: [24,12,8,6]

Tip: The problem data ensures that the product of all prefixes and suffixes (or even the entire array) of any element in the array is within the range of 32-bit integers. Note: Please do not use division and complete this problem in O(n) time complexity. Advanced: Can you do this problem in constant space complexity? (For the sake of spatial complexity analysis, the output array is not considered extra space.)

Ii. Analysis of Ideas:

Methods a

It’s traversed twice

The first pass computes the previous product of the current element and saves the result in the list. like

List [3] = 1 *2;

List [4] = 1 * 2 * 3;

The second iteration iterates backwards, calculating the product of the current element and multiplying it by list[I]. Here’s an example:

When I = 4, list[4] = 1 * 2 *3, then *1;

When I = 3, list[4] = 1 * 2, then *1 * 4;

When I = 2, list[4] = 1, then *1 * 4 * 3;

When I = 1, list[4] = 1, then *1 * 4 * 3 * 2;

Method 2

It’s violence. The two for loops are very simple, but they run out of time

Iii. AC Code:

/ * * *@param {number[]} nums
 * @return {number[]}* /
var productExceptSelf = function(nums) {
    let list = [], k = 1;
    for(let i = 0; i < nums.length; i++) { list[i] = k; k = nums[i] * k; } k =1;
    for(let i = nums.length - 1; i >=0; i--) { list[i] = list[i] * k; k = k * nums[i] }return list;
};
Copy the code
/ * * *@param {number[]} nums
 * @return {number[]}* /
var productExceptSelf = function(nums) {
    The second for loop finds values that are not the first for loop and multiplies them one by one */
    let list = [];
    for(let i =0; i < nums.length; i++) {let num = 1;
        for(let j = 0; j < nums.length; j++) {
            if(i ! == j) { num = num * nums[j] } } list.push(num); }return list;
};
Copy the code

Iv. Summary:

The first solution can be found in leetcode-cn.com/problems/pr…

Brush the fourth day, working people refueling!! This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign