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):题目说明了返回的那个数组不算进复杂度分析里 ...
随机推荐
- spring-data-mongodb必须了解的操作
http://docs.spring.io/spring-data/data-mongo/docs/1.0.0.M5/api/org/springframework/data/mongodb/core ...
- ARM指令集——数据处理指令
ARM汇编指令集 ARM汇编文件的组成 指令:编译完成后作为一条指令(机器码)存储在内存单元中,CPU执行时能够完成处理的操作 伪指令:在编译时替换成能被识别的ARM指令 伪操作:知道编译器进行编译, ...
- XML配置silverlight ,wcf 解析xml
XML 代码: <?xml version="1.0" encoding="utf-8" ?><ChartSet xmlns:xsi=&qu ...
- C#创建Windows服务的几个注意事项
1.服务安装后的自动启动:服务的StartType即使配置成Automatic,在首次安装成功之后还是要在服务列表中找到并手工启动.此外,可以通过在ProjectInstaller中添加AfterIn ...
- 使用NSURLCache为NSURLRequest设置缓存
http://m.blog.csdn.net/blog/u011439689/18734363#
- 对plist文件的简单封装
常常会用到对plist文件的封装 +(NSArray *)LoadFriendsDataFromPlist:(NSString *)plistName{ NSString * filePath = [ ...
- 一个cocos2d-x的项目
前几天完成了一个cocos2d-x的项目,放在git上: https://github.com/gittor/Jigsaw 采用cocos的版本是3.7.1. 项目是一个拼图的游戏,市面上的拼图类游戏 ...
- org.hibernate.service.jndi.JndiException: Error parsing JNDI name []
我的hibernate.cfg.xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <!DOCTY ...
- 滚动到指定元素的id处+当元素出现在浏览器显示区域就会自动加载
//滚动到指定元素的id处 如:$("#Exam82") function Jump() { var scroll_offset = $("#Exam82"). ...
- 使用json常用到的包有以下六个
使用json常用到的包有以下六个 1. commons-logging-1.0.4.jar 2. commons-lang-2.3.jar 3. commons-collections-3.2.jar ...