给出一个数组 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. UCloud上LAMP小型站点搭建与測试

    文件夹 介绍 LAMP环境搭建 打开UCloud防火墙 WordPress安装 应用測试 介绍 本篇博客旨在通过介绍搭建一个WordPress博客的过程介绍在UCloud的云主机(UHOST)上搭建单 ...

  2. thinkphp自动完成、软删除 和时间戳

    thinkphp自动完成.软删除 和时间戳 一.总结 自动完成:没有手动赋值的情况下进行手动处理 软删除:实现假删除,可以进行恢复 时间戳:系统支持自动写入创建和更新的时间戳字段 二.thinkphp ...

  3. Spring Cloud项目

    如何使用windows版Docker并在IntelliJ IDEA使用Docker运行Spring Cloud项目   如何使用windows版Docker并在IntelliJ IDEA使用Docke ...

  4. Unity(IOC)学习笔记

    原文:Unity(IOC)学习笔记 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/details/79432 ...

  5. Android使用BitmapFactory.Options解决加载大图片内存溢出问题

    由于Android对图片使用内存有限制,若是加载几兆的大图片便内存溢出.Bitmap会将图片的所有像素(即长x宽)加载到内存中,如果图片分辨率过大,会直接导致内存溢出(java.lang.OutOfM ...

  6. 如何在 Linux 中统计一个进程的线程数

    编译自:http://ask.xmodulo.com/number-of-threads-process-linux.html作者: Dan Nanni原创:LCTT https://linux.cn ...

  7. Java 字符转Unicode

    static String unicode2String(String unicodeStr) { StringBuffer sb = new StringBuffer(); String str[] ...

  8. 【50.00%】【codeforces 602C】The Two Routes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. ios开发网络学八:NSURLSession相关代理方法

    #import "ViewController.h" @interface ViewController ()<NSURLSessionDataDelegate> /* ...

  10. css3-9 css中的浮动怎么使用

    css3-9 css中的浮动怎么使用 一.总结 一句话总结:用来做一般的行效果,比如说手机左右分布的头部导航栏.浮动的东西放到一个div中去,里面的内容根据需求左浮动或者右浮动,然后记得加上清除浮动. ...