【刷题-LeetCode】238. Product of Array Except Self
- Product of Array Except Self
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.)
解 left-right。计算第i个数左边和右边的乘积
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int>ans;
vector<int>left(nums.size(), 1), right(nums.size(), 1);
for(int i = 1; i < nums.size(); ++i){
left[i] = left[i-1] * nums[i-1];
}
for(int i = nums.size()-1; i > 0; --i){
right[i-1] = right[i]*nums[i];
}
for(int i = 0; i < nums.size(); ++i){
ans.push_back(left[i]*right[i]);
}
return ans;
}
};
优化:在从右往左扫描时,right数组中的数字只用了一次,因此用一个变量R代替即可
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int>ans(nums.size(), 1);
for(int i = 1; i < nums.size(); ++i){
ans[i] = ans[i-1] * nums[i-1];
}
int R = 1;
for(int i = nums.size()-1; i >= 0; --i){
ans[i] *= R;
R *= nums[i];
}
return ans;
}
};
【刷题-LeetCode】238. Product of Array Except Self的更多相关文章
- LN : leetcode 238 Product of Array Except Self
lc 238 Product of Array Except Self 238 Product of Array Except Self Given an array of n integers wh ...
- 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)
剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...
- [LeetCode] 238. Product of Array Except Self 除本身之外的数组之积
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ ...
- LeetCode 238. Product of Array Except Self (去除自己的数组之积)
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- (medium)LeetCode 238.Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- Java [Leetcode 238]Product of Array Except Self
题目描述: Given an array of n integers where n > 1, nums, return an array output such that output[i] ...
- C#解leetcode 238. Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- [leetcode]238. Product of Array Except Self除了自身以外的数组元素乘积
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ ...
- leetcode 238 Product of Array Except Self
这题看似简单,不过两个要求很有意思: 1.不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨. 2.空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里 ...
- [leetcode] 238. Product of Array Except Self (medium)
原题 思路: 注意时间复杂度,分别乘积左右两边,可达到O(n) class Solution { public: vector<int> productExceptSelf(vector& ...
随机推荐
- CF981B Businessmen Problems 题解
Content 有一个长度为 \(n\) 的序列和长度为 \(m\) 的序列,两个序列中的元素都有一个编号 \(num\) 和一个值 \(val\),且同一个序列的元素之间的编号互不相同.现在从这两个 ...
- 使用Redis+自定义注解实现接口防刷
最近开发了一个功能,需要发送短信验证码鉴权,考虑到短信服务需要收费,因此对此接口做了防刷处理,实现方式主要是Redis+自定义注解(需要导入Redis的相关依赖,完成Redis的相关配置,gs代码,这 ...
- java 图形化小工具Abstract Window Toolit :画笔Graphics,画布Canvas(),弹球小游戏
画笔Graphics Java中提供了Graphics类,他是一个抽象的画笔,可以在Canvas组件(画布)上绘制丰富多彩的几何图和位图. Graphics常用的画图方法如下: drawLine(): ...
- 重学c#系列——string.empty 和 "" 还有null[二十]
前言 简单整理一下string.empty 和 "" 还有 null的区别. 正文 首先null 和 string.empty 还有 "" 是不一样的. nul ...
- video标签实现多个视频循环播放
<head> <!-- If you'd like to support IE8 (for Video.js versions prior to v7) --> </he ...
- c++设计模式概述之观察者
代码写的不够规范,目的是缩短篇幅,实际情况请不要这样做 1.概述 观察者模式,类比生活中的场景,比如看电影,观众对播放的内容有不同的反应, 再比如订阅,公众号订阅,只要你订阅了其公众号,你就会收到其推 ...
- 【九度OJ】题目1169:比较奇偶数个数 解题报告
[九度OJ]题目1169:比较奇偶数个数 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1169 题目描述: 第一行输入一个数,为n, ...
- 【九度OJ】题目1442:A sequence of numbers 解题报告
[九度OJ]题目1442:A sequence of numbers 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1442 ...
- 【LeetCode】777. Swap Adjacent in LR String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 智商题 相似题目 参考资料 日期 题目地址:http ...
- GCD - Extreme (II)(UVA11426)
思路:欧拉函数: 欧拉函数,然后用下等差数列公式就行了. 1 #include<stdio.h> 2 #include<algorithm> 3 #include<ios ...