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):题目说明了返回的那个数组不算进复杂度分析里 ...
随机推荐
- 解决安装mysql-connector-odbc-5.3.2 错误1918……不能载入安装或转换器库……的BUG
还是在虚拟机Windows Server 2003上安装mysql-connector-odbc-5.3.2,装着装着就报错了,大致是"错误1918--不能载入安装或转换器库--" ...
- css3-10 如何使用滚动条
css3-10 如何使用滚动条 一.总结 一句话总结:给设置了宽高的块标签使用,直接将overflow属性写到style里面即可. 1.滚动条的使用对象时谁? 一般是div,当div比较小(设置了宽高 ...
- stm32的ADC规则组通道采样顺序设置
先看一下固件库手册 再看一下手册上的例子: 有两个通道,,并且顺序如下
- 7 Best jQuery & JavaScript PDF Viewer plugin with examples
In this Post we are providing best jQuery PDF viewer plugin & tutorial with examples.Due to popu ...
- JSON序列化自己主动过滤NULL值
使用Newtonsoft.Json.dll 序列化为json时主动将NULL值过滤掉.详细做法: var jSetting = new JsonSerializerSettings {NullValu ...
- Android中常用的优秀开源框架
Android开源框架库分类,挑选出最常用,最实用的开源项目,本篇主要介绍的是优秀开源框架库和项目,UI个性化控件会独立介绍.UI个性化控件 Index Dependency Injections A ...
- 用Ajax图片上传、预览、修改图片
首选图片的上传和下载并不是很难,但要注意细节. 一,给出前端图片上传的html代码 1.图片上传的控件 <img src="/${res}/images/default.png&quo ...
- USB 3.0规范中译本 第3章 USB 3.0体系结构概览
本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 本章呈现USB 3.0体系结构和关键概念的概览.USB 3.0与前面版本的USB类似,因为它是线缆总线,支持 ...
- 【9107】Hanoi双塔问题(NOIP2007)
Time Limit: 10 second Memory Limit: 2 MB 问题描述 给定A,B,C三根足够长的细柱,在A柱上放有2n个中间有孔的圆盘,共有n个不同的尺寸,每个尺寸都有两个相同的 ...
- 百度echarts可以做什么
百度echarts可以做什么 一.总结 一句话总结:可视化做的很好,各种图都有.而且支持动态数据. 二.百度eCharts体验 前言 从昨天开始给项目里添加一些图表对比功能,上一个项目里使用的是Hig ...