题目
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Solve it without division and in O(n).
For example, given [1,2,3,4], return [24,12,8,6].
Follow up:
Could you solve it with constant space complexity?
(Note: The output array does not count as extra space
for the purpose of space complexity analysis.)
思路
对于i = 5 时 result[5] = (nums[0] * nums[1] * nums[2] * nums[3] * nums[4] ) * (nums[6] nums[7] * nums[8] * nums[9] * nums[10])
从上面开始看出对于第i个,我们只要知道它左边的连续乘积 和 它右边的连续乘积 就OK了。
代码
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int size = nums.size();
vector<int> result(size,0);
vector<int> left(size,0);
vector<int> right(size,0);
int leftNum = 1,rightNum = 1;
for(int i = 0;i < size;++i){
left[i] = leftNum;
right[size-i-1] = rightNum;
leftNum *= nums[i];
rightNum *= nums[size-i-1];
}
for(int i = 0;i < size;++i){
result[i] = left[i] * right[i];
}
return result;
}
};
int main(){
Solution s;
vector<int> vec = {1,2};
vector<int> result = s.productExceptSelf(vec);
int size = result.size();
for(int i = 0;i < size;++i){
cout<<result[i]<<" ";
}
return 0;
}
运行时间
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
分享到:
相关推荐
思路:题目中说不能用除法;用暴力,会超时。 为了计算 除第i个元素的乘积,可以将数组分为三个部分:1…i-1; i; i+1…n (注:这里的i代表序号,不是数组下标) output[i] = a * b[被乘数a:代表前i-1个数的乘积...
python python_leetcode题解之238_Product_of_Array_Except_Self.py
lru缓存leetcode 我们使用python来处理关于leetcode和linkcode的话题。 leetcode 网址 = 链接代码网址 = 大致分为几个主题: ————————————————————————————————————————...
318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-...
这是来自LeetCode的已解决任务的存储库使用Java语言解决任务 CoinChange.java - //leetcode....
leetcode category other hot keywords:Palindrome(mic), Subsequence Array 螺旋矩阵Spiral Matrix 顺时针打印矩阵 Next Permutation Product of Array Except Self 189.rotate-array 283.move-zero Range Sum ...
Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...
13. **Product of Array Except Self**:不包含自身的数组乘积。可以使用前缀和和后缀乘积的概念,结合位运算优化计算。 14. **Invert Binary Tree**:翻转二叉树。这可以通过递归实现,每个节点的左子树和右子树...
lru缓存leetcode 力扣_30天 力扣 30 天挑战赛 日 问题描述 问题和解决方案链接 Git 解决方案页面 1 SINGLE NUMBER 2 HAPPY NUMBER 3 MAXIMUM SUBARRAY 4 Move Zeroes 5 Best Time to Buy and Sell Stock II 6 GROUP ...
- Product of Array Except Self(数组中除了自身以外的乘积) - Partition Array(分割数组) - First Missing Positive(缺失的第一个正数) - 2 Sum(两数之和) - 3 Sum(三数之和) - 3 Sum Closest(三...