We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difficulty: 中等
Related Topics: 数组, 前缀和
给你一个整数数组 nums,返回 数组 answer ,其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。
nums
answer
answer[i]
nums[i]
题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。
请**不要使用除法,**且在 O(_n_) 时间复杂度内完成此题。
O(_n_)
示例 1:
输入: nums = [1,2,3,4] 输出: [24,12,8,6]
示例 2:
输入: nums = [-1,1,0,-3,3] 输出: [0,0,9,0,0]
提示:
-30 <= nums[i] <= 30
进阶:你可以在 O(1) 的额外空间复杂度内完成这个题目吗?( 出于对空间复杂度分析的目的,输出数组不被视为额外空间。)
O(1)
Language: JavaScript
/** * @param {number[]} nums * @return {number[]} */ // 从左往右遍历: 记录从左到当前位置前一位的乘积 // 从右往左遍历: 从左到当前位置前一位的乘积 乘上 右边元素的积 var productExceptSelf = function(nums) { const res = [] res[0] = 1 for (let i = 1; i < nums.length; i++) { res[i] = res[i - 1] * nums[i - 1] } let right = 1 for (let j = nums.length - 1; j >= 0; j--) { res[j] *= right right *= nums[j] } return res };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
238. 除自身以外数组的乘积
Description
Difficulty: 中等
Related Topics: 数组, 前缀和
给你一个整数数组
nums
,返回 数组answer
,其中answer[i]
等于nums
中除nums[i]
之外其余各元素的乘积 。题目数据 保证 数组
nums
之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。请**不要使用除法,**且在
O(_n_)
时间复杂度内完成此题。示例 1:
示例 2:
提示:
-30 <= nums[i] <= 30
nums
之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内进阶:你可以在
O(1)
的额外空间复杂度内完成这个题目吗?( 出于对空间复杂度分析的目的,输出数组不被视为额外空间。)Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: