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):题目说明了返回的那个数组不算进复杂度分析里 ...
随机推荐
- Android Studio使用技巧系列教程(二)
尊重劳动成果,转载请注明出处:http://blog.csdn.net/growth58/article/details/46764575 关注新浪微博:@于卫国 邮箱:yuweiguocn@gmai ...
- (转)sql中 in 、not in 、exists、not exists 用法和差别
exists (sql 返回结果集为真) not exists (sql 不返回结果集为真) 如下: 表A ID NAME 1 A1 2 A2 3 A3 表B ID AI ...
- 一个Banner广告收缩效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- EasyUI layout动态设置Split属性
switch (rowData.ISREGISTERTASK) { case 0: 显示north分割线 $('#RightContent').la ...
- 《第一行代码》学习笔记18-广播接收器Broadcast_Receiver(1)
1.网络通信原理,在一个IP网络范围内最大的IP地址是被保留作为广播地址来使用的.某个网络的IP 范围是192.168.0.XXX, 子网掩码是255.255.255.0,则该网络的广播地址是192. ...
- createjs基础
<canvas id="gameView" width="400px" height="400px" style="back ...
- iOS RGB颜色封装
使用类别创建 .h文件 #import <UIKit/UIKit.h> @interface UIColor (HexColor) + (UIColor *)colorWithHex:(N ...
- Objective-C set/get方法
主要内容set get方法的使用 关键字 @property 全自动生成set get方法 // 类的声名 @interface People : NSObject{ int _age; // 成员变 ...
- 对于JavaScript对象的prototype和__proto__的理解
一.Object和Function的关系: 刚学JavaScript的时候,看书上说JavaScript中万物皆对象,而javascript中的其他对象都是从Object继承而来,包括内置对象.瞬间觉 ...
- WordPress nginx环境下开启多站点
在wp-config.php插入 define('WP_ALLOW_MULTISITE', true); 进入管理页面安装网络,子目录模式按提示再在wp-config.php插入 define('MU ...