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):题目说明了返回的那个数组不算进复杂度分析里 ...
随机推荐
- [Angular 2] Using the @Inject decorator
TypeScript is used heavily as we build up our application, but TypeScript isn’t required. If you wan ...
- Android 编程下 Activity 的创建和应用退出时的销毁
为了确保对应用中 Activity 的创建和销毁状态进行控制,所以就需要一个全局的变量来记录和销毁这些 Activity.这里的大概思路是写一个类继承 Application,并使获取该 Applic ...
- samba服务器详细配置(非域模式)
组成Samba运行的有两个服务,一个是SMB,另一个是NMB:SMB是Samba 的核心启动服务,主要负责建立Samba服务器与Samba客户机之间的对话,验证用户身份并提供对文件和打印系统的访问,只 ...
- linux加入windows域之完美方案(转载)
概念理解:1.kdc:可信任的密钥分发中心(KDC, Key Distribution Center).2.Winbind是Samba套件的功能之一.它允许Unix系统利用Windows NT的用户帐 ...
- css画下图
通常我看到这种效果,都是直接ps解决,但是不断重申性能的今天,显然不适应时代的需求啊! 今天看到群里有人问这种效果怎么做了,我在思考的时候,有人已经给出答案了: 我就测试了一下,发现确实可以实现,总结 ...
- 《第一行代码》学习笔记16-碎片Fragment(1)
1.碎片( Fragment):一种可以嵌入在活动当中的UI片段,能让程序更加合理和充分地利用大屏幕的空间,在平板上的应用广泛. 2.碎片同样包括布局,有自己的生命周期,甚至可理解成一个迷你型的活动. ...
- OpenCV——手势识别
使用ANN神经网络训练数据后进行手势识别. #include "header.h" int main() { ; //训练每类图片数量 ; //训练类数3:石头剪刀布 ; ; st ...
- socket网络编程中的同步,异步,阻塞式,非阻塞式,有何联系与区别?
一.举个打电话的例子: 阻塞 block 是指,你拨通某人的电话,但是此人不在,于是你拿着电话等他回来,其间不能再用电话.同步大概和阻塞差不多. 非阻塞 nonblock 是指,你拨通 ...
- Small factorials Solved Problem code: FCTRL2
import sys def fact(n): final = n while n > 1: final *= n - 1 n -= 1 return final #逻辑严谨,不要忘了retur ...
- WebApi 能支持Session
由于项目实际需要,我希望让WebApi服务也能支持Session,所以便查找资料按照网上的方法开始着手实验. 然后就有了以下的代码,主要是说让WebApi支持Session,要重写Global.asa ...