0
点赞
收藏
分享

微信扫一扫

LeetCode_Array_238. Product of Array Except Self 除自身以外数组的乘积(C++/Java)


目录

​​一,题目描述​​

​​英文描述​​

​​中文描述​​

​​二,解题思路​​

​​三,AC代码​​

​​C++​​

​​Java​​

​​四,解题过程​​

​​第一博​​

​​第二搏​​

​​第三搏​​

一,题目描述

原题链接​​238. 除自身以外数组的乘积​​

英文描述

Given an array nums of n integers where n > 1,  return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Example:

Input:  [1,2,3,4]
Output: [24,12,8,6]
Constraint: It's guaranteed that the product of the elements of any prefix or suffix of the array (including the whole array) fits in a 32 bit integer.

Note: Please solve it without division and in O(n).

Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)

中文描述

给你一个长度为 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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

二,解题思路

ans数组存放最终答案,left记录当前位置左边元素之积,right记录当前位置右边元素之积;

以上变量全部初始为1;

遍历数组一次,每次循环将ans中相应位置分别乘以 left 和 right (ans[i] *= left ;ans[j] *= right;),并更新left和right(left *= nums[i] ; right *= nums[j]);

 

三,AC代码

C++

class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> ans(nums.size(), 1);
int n = nums.size(), left = 1, right = 1;
for(int i = 0, j = n - 1; i < n; i++, j--) {
ans[i] *= left;
left *= nums[i];

ans[j] *= right;
right *= nums[j];
}
return ans;
}
};

Java

class Solution {
public int[] productExceptSelf(int[] nums) {
int[] ans = new int[nums.length];
int n = nums.length, left = 1, right = 1;
for(int i = 0; i < n; i++) ans[i] = 1;
for(int i = 0, j = n - 1; i < n; i++, j--) {
ans[i] *= left;
left *= nums[i];

ans[j] *= right;
right *= nums[j];
}
return ans;
}
}

四,解题过程

第一博

三次遍历数组。

  • 从左向右遍历,ans[i]记录当前元素左边所有元素之积;
  • 从右向左遍历,tem[i]记录当前元素右边所有元素之积;
  • 最后遍历一次,将两个数组对应位置元素相乘即可ans[i] = ans[i] * tem[i];

class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> ans(nums.size(), 1);
vector<int> tem(nums.size(), 1);
for(int i = 1; i < nums.size(); i++) {
tem[i] = tem[i - 1] * nums[i - 1];
}
for(int i = nums.size() - 2; i >= 0; i--) {
ans[i] = ans[i + 1] * nums[i + 1];
}
for(int i = 0; i < nums.size(); i++) {
ans[i] *= tem[i];
}
return ans;
}
};

惨不忍睹的效率:时间O(N),空间O(N)

LeetCode_Array_238. Product of Array Except Self 除自身以外数组的乘积(C++/Java)_Array

第二搏

两次遍历数组;

  • 从左向右遍历,ans[i]记录当前元素左边所有元素之积;
  • 从右向左遍历,tem记录当前元素nums[i]右边所有元素之积,更新ans[i] *= tem; tem *= nums[i];

这样时间为O(N),空间O(1)(按照题目说明,答案的数组不算做占用的空间)

class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> ans(nums.size(), 1);
int n = nums.size(), tem = nums[n - 1];
for(int i = 1; i < n; i++) {
ans[i] = ans[i - 1] * nums[i - 1];
}
for(int i = n - 2; i >= 0; i--) {
ans[i] *= tem;
tem *= nums[i];
}

return ans;
}
};

然鹅o( ̄┰ ̄*)ゞ

LeetCode_Array_238. Product of Array Except Self 除自身以外数组的乘积(C++/Java)_数组_02

第三搏

思路受到局限了,既然从右到左累乘结果可以用一个变量代替,那么从左到右为什么不能?

为什么要按照顺序累乘,先从左到右乘一遍,再从右到左乘一遍?左右同时开工不香吗?

class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> ans(nums.size(), 1);
int n = nums.size(), left = 1, right = 1;
for(int i = 0, j = n - 1; i < n; i++, j--) {
ans[i] *= left;
left *= nums[i];

ans[j] *= right;
right *= nums[j];
}
return ans;
}
};

然鹅,难以置信耗时居然还变多了o( ̄┰ ̄*)ゞ

LeetCode_Array_238. Product of Array Except Self 除自身以外数组的乘积(C++/Java)_LeetCode_03

举报

相关推荐

0 条评论