给出一个数组 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 递推的更多相关文章

  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 ...

  2. 剑指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] * ...

  3. [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 ...

  4. 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 ...

  5. (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 ...

  6. 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]  ...

  7. 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 ...

  8. [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 ...

  9. leetcode 238 Product of Array Except Self

    这题看似简单,不过两个要求很有意思: 1.不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨. 2.空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里 ...

随机推荐

  1. iOS开发 - Quartz2D画图

    Quartz 2D简单介绍 是一个二维画图引擎,同一时候支持iOS和Mac系统 Quartz 2D能完毕的工作 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成 ...

  2. Java反射学习总结一(基础篇)

    Class类是Reflection API中核心的类,他位于Java.lang.Class 列出一些常用的方法. - getName() : 获得类的完整名字 - getFields() : 获得类的 ...

  3. 5、linux下应用字符串相关调用函数列举说明

    1.函数原型int strcmp(const char *s1,const char *s2);设这两个字符串为s1,s2,规则当s1<s2时,返回为负数当s1=s2时,返回值= 0当s1> ...

  4. Android屏幕信息获取

    Android中有时需要获取屏幕的size信息以便对控件位置进行动态控制,最近做了一些研究,现在将获取屏幕大小信息的方法总结如下,可能存在一些地方理解的不全面. 1.getMetrics Displa ...

  5. js进阶 12-3 如何实现元素跟随鼠标移动

    js进阶 12-3 如何实现元素跟随鼠标移动 一.总结 一句话总结:获取鼠标位置,将鼠标位置设置为元素偏移即可. 1.用什么事件获取鼠标位置? 用mousemove可以获取鼠标移动的时候的位置 $(d ...

  6. Linux 如何查看一个进程的堆栈

    有两种方法: 第一种:pstack 进程ID 第二种,使用gdb 然后attach 进程ID,然后再使用命令 thread apply all bt 第三种:strace -f -p pid  该方法 ...

  7. Win10安装后必做的优化,解决磁盘100%占用

    Win10安装后必做的优化,解决磁盘100%占用 01关闭家庭组 控制面板–管理工具–服务– HomeGroup Listener和HomeGroup Provider禁用. 02关闭磁盘碎片整理.自 ...

  8. 安装Centos时“sda必须有一个GPT磁盘标签”

    http://jingyan.baidu.com/article/c45ad29c272326051753e2d1.html

  9. csdn博客栏目加入微博关注

    大家首先切换到:博客专栏,然后点击"加入专栏".然后直接复制下述代码就能够了: <a href="http://weibo.com/u/3247569660/hom ...

  10. php中foreach源码分析(编译原理)

    php中foreach源码分析(编译原理) 一.总结 编译原理(lex and yacc)的知识 二.php中foreach源码分析 foreach是PHP中很常用的一个用作数组循环的控制语句.因为它 ...