Leetcode 238 Product of Array Except Self 递推
给出一个数组 nums[i](i = 0,1,...,n-1) 输出数组output[i]满足 output[i] = nums[0] * num[1] * num[2] *..*num[i-1] * num[i+1]*... *num[n-1]
要求不能使用除了output之外的大内存,满足时间复杂度O(n), 空间复杂度O(1)。
分析下我们可以令dpf[i] = nums[0] * num[1] * num[2] *..*num[i-1] 而 dpb[i] = num[i+1]*... *num[n-1];
而output[i] = dpf[i] * dpb[i];dpd[i]可以在作为常数mul出现,而dpf[i]可以预存在 output[i]中。
注意:当n=1时的判断。
class Solution {
public:
std::vector<int> productExceptSelf(std::vector<int>& nums) {
std::vector<int> output(nums.size(), );
if(nums.size() == ) return nums;
for(std::vector<int>::size_type i = ; i < nums.size(); ++i){
output[i] *= output[i-] * nums[i -];
}
int mul = ;
for(int i = nums.size() - ; i >= ; --i){
mul *= nums[i + ];
output[i] *= mul;
}
return output;
}
};
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):题目说明了返回的那个数组不算进复杂度分析里 ...
随机推荐
- Maven基础教程 分类: C_OHTERS 2015-04-10 22:53 232人阅读 评论(0) 收藏
更多内容请参考官方文档:http://maven.apache.org/guides/index.html 官方文档很详细,基本上可以查找到一切相关的内容. 另外,快速入门可参考视频:孔浩的maven ...
- 前端项目课程3 jquery1.8.3到1.11.1有了哪些新改变
web项目课程3 jquery1.8.3到1.11.1有了哪些新改变 一.总结 一句话总结:领会官方升级的意思. 1.live(); 2.die(); 3.bind(); 4.u ...
- JS-OO-数据属性,访问器属性
一.数据属性 Configurable:表示能否通过Delete删除属性从而重新定义属性,能否修改属性的特性,能否把属性修改为访问器属性.默认true. Enumerable:表示能否通过for-in ...
- [Immutable.js] Updating nested values with ImmutableJS
The key to being productive with Immutable JS is understanding how to update values that are nested. ...
- Java虚拟机解析篇之---内存模型
今天闲来无事来,看一下Java中的内存模型和垃圾回收机制的原理.关于这个方面的知识,网上已经有非常多现成的资料能够供我们參考,可是知识还是比較杂的,在这部分知识点中有一本书不得不推荐:<深入理解 ...
- TextView之二:常用属性 分类: H1_ANDROID 2013-10-30 12:43 3203人阅读 评论(0) 收藏
参考自<疯狂android讲义>2.3节 //TextView所呈现的文字 android:text="我爱Java" //文字颜色 android:textColor ...
- 【b802】火柴棒等式
Time Limit: 1 second Memory Limit: 50 MB [问题描述] 给你n根火柴棍,你可以拼出多少个形如"A+B=C"的等式?等式中的A.B.C是用火柴 ...
- Linux下停Tomcat服务器,出现Connection refused错误解决办法
错误内容如下 : 2010-9-19 16:09:58 org.apache.catalina.startup.Catalina stopServer 严重: Catalina.stop: java. ...
- [RxJS] Flatten a higher order observable with mergeAll in RxJS
Among RxJS flattening operators, switch is the most commonly used operator. However, it is important ...
- Android studio在Refresh gradle project卡死,附解决办法
首先打开android studio项目 找到项目目录gradle\wrapper\gradle-wrapper.properties这个文件 你会看到 #Wed Apr 10 15:27:10 PD ...