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):题目说明了返回的那个数组不算进复杂度分析里 ...
随机推荐
- 关于Linux启动时挂载rootfs的几种方式
一直对Linux启动时挂载根文件系统的过程存在着很多疑问,今天在水木精华区找到了有用的资料,摘录如下: 1.Linux启动时,经过一系列初始化之后,需要mount 根文件系统,为最后运行init进程等 ...
- 关于Topsort
Long time no see. 拓扑排序 英文名称:Topological-sort 别称:toposort or topsort 拓扑排序是干什么的呢 对一个有向无环图(Directed Ac ...
- Vim技巧之四大模式_插入模式
Vim技巧之四大模式_插入模式 在插入模式中及时更正错误 插入-普通模式 在插入模式模式以下直接粘贴指定寄存器的内容 插入模式中做运算 用字符编码插入很常使用字符 替换已有的文本 Vim技巧之四大模式 ...
- Linux system函数返回值
例: status = system("./test.sh"); 1.先统一两个说法: (1)system返回值:指调用system函数后的返回值,比如上例中status为syst ...
- Vue源码--解读vue响应式原理
原文链接:https://geniuspeng.github.io/2018/01/05/vue-reactivity/ Vue的官方说明里有深入响应式原理这一节.在此官方也提到过: 当你把一个普通的 ...
- Struts2完全解耦和
jsp: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnco ...
- 超级简单的Android Studio jni 实现(无需命令行)
1.配置Anroid Studio(这步是关键) 使用[command+,] 打开Preferences,选择External Tools,点击加号框如下图: Paste_Image.png 点击+号 ...
- c# 安全队列
using System;using System.Collections.Concurrent;using System.Collections.Generic;using System.Linq; ...
- JS null问题
在学习getElementById()方法的过程中出现了这样一个问题,便想记录下来. 分析问题之前,我们最好还是先来认识一下getElementById()方法.getElementById()方法, ...
- UE4.5.0的Kinect插件(Plugin)<一>
声明:所有权利保留. 转载必须说明出处:http://blog.csdn.net/cartzhang/article/details/43193431 UE4 Plugin,在UE4的官网,放出了有个 ...