Leetcode 238 Product of Array Except Self 时间O(n)和空间O(1)解法
1. 问题描写叙述
给定一个n个整数的数组(n>1)nums,返回一个数组output,当中的元素outputi的值为原数组nums中除numsi之外的全部元素的积。比如:nums数组为[1,2,3,4]。返回的output数组为[24,12,8,6]。
要求不用除法和时间复杂度为O(n).
2. 方法与思路
这道题假设没有除法的限制的话就非常easy了,先求全部数的乘积,然后除以numsi。考虑一下除数为零的情况,非常好解决。
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> re;
long mul=1;
int zeroNum = 0;
for(int i=0; i < nums.size(); i++)
{
if(nums[i] != 0) mul *= nums[i];
else zeroNum++;
}
for(int i = 0; i < nums.size(); i++)
{
if(zeroNum > 1) re.push_back(0);
else if(zeroNum == 1)
{
if(nums[i] == 0) re.push_back(mul);
else re.push_back(0);
}
else
re.push_back(mul/nums[i]);
}
return re;
}
};
可是题目上明白要求不能用除法,那我们得另辟蹊径了。以下以数组[1,2,3,4,5]为例,看完以下表述就明白了:
| 扫描顺序 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| 从左至右 | 1 | 1×1 | 1×1×2 | 1×1×2×3 | 1×1×2×3×4 |
| 从右至左 | 1×(2×3×4×5) | (1×1)×(3×4×5) | (1×1×2)×(4×5) | (1×1×2×3)×(5) | (1×1×2×3×4)×(1) |
就是先从左至右扫描,记录前i−1个数的乘积,第二遍扫描时,从右至左。将乘积再乘以后i+1位的乘积。
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> re(nums.size(),1);
int i,tmp = 1;
for(i = 1; i < nums.size(); i++)
re[i] =re[i-1] * nums[i-1];
for(i = nums.size()-1; i >= 0; i--)
re[i] *= tmp,tmp *= nums[i];
return re;
}
};
Leetcode 238 Product of Array Except Self 时间O(n)和空间O(1)解法的更多相关文章
- 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):题目说明了返回的那个数组不算进复杂度分析里 ...
随机推荐
- PHP 生成.csv 文件并下载到浏览器
近期做了一个项目须要把订单的信息显示出来.而且可以把相关信息放到一个.csv 文件里,下载到浏览器.首先我要说明的是.csv 文件,PHP 有专门的函数去解析该类型的文件,相关函数大家可以去官网查看. ...
- Linux环境下Eclipse + Tomcat + MySQL 配置J2EE开发环境的方法
1. 版本号信息 (1)CentOS 6.4发行版64位,uname -a 显演示样例如以下: Linux localhost.localdomain 3.11.6 #1 SMP Sat Nov 2 ...
- 执行curl -sSL 提示curl: (35) SSL connect error
今天,添加容器节点报错,执行如下 curl -sSL https://shipyard-project.com/deploy| ACTION=node DISCOVERY=etcd://192.168 ...
- (转) 值不能为空。参数名viewinfo(microsoft.sqlserver.management.sqlstudio.explorer)
打开MSSQL 2008 R2的时候,展开数据库都显示以下的错误提示: 值不能为空.参数名viewinfo(microsoft.sqlserver.management.sqlstudio.explo ...
- js处理数学经典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一 对兔子,假如兔子都不死,问每个月的兔子总数为多少?
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- (二)原生JS实现 - 事件类方法
事件处理 - 添加事件 var addEventHandler = function (oTarget, sEventType, fnHandler) { if (oTarget.addEventLi ...
- BZOJ 2330 SCOI 2011 糖果
2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MB Description 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友 ...
- 一个cocos2d-x的项目
前几天完成了一个cocos2d-x的项目,放在git上: https://github.com/gittor/Jigsaw 采用cocos的版本是3.7.1. 项目是一个拼图的游戏,市面上的拼图类游戏 ...
- hdu5353 Average(模拟)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Average Time Limit: 4000/2000 MS (Java/Ot ...
- CSS常见BUG
CSS Hack IE条件注释: 所有IE:<!--[if IE]> css code <![endif]--> IE6以上:<!--[if gt IE 6]> c ...