Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Solve it without division and in O(n).

For example, given [1,2,3,4], return [24,12,8,6].
由于不允许用除法,而且事件复杂度应该为O(N),可以想象到结果数组中摸个位置的只等于起对应nums所有的左边元素以及右边元素相乘即可,那么分两次,一次从右向左一次从左向右就可以完成乘机的计算:

 class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> res;
int sz = nums.size();
res.resize(sz);
res[sz - ] = ;
for(int i = sz - ; i >= ; --i){
res[i] = res[i + ] * nums[i + ];
}
int left = ;
for(int i = ; i < sz; ++i){
res[i] *= left;
left *= nums[i];
}
return res;
}
};

LeetCode OJ:Product of Array Except Self(除己之外的元素乘积)的更多相关文章

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

  2. 【LeetCode】Product of Array Except Self

    Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...

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

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

  5. [LintCode] Product of Array Except Self 除本身之外的数组之积

    Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WI ...

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

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

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

随机推荐

  1. Python高级教程-生成器

    生成器(Generator) 通过列表生成式,可以直接创建一个列表.但是,受内存限制,列表的容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几 ...

  2. HTML中的SEO和HTML语义化

    SEO 1) <title>网站SEO标题</title>, 百度搜索出来的记录, 其标题基本就提取至网站的title, 标签, 因此标题起的好, 不论对点击率还是SEO都至关 ...

  3. python数据可视化、数据挖掘、机器学习、深度学习 常用库、IDE等

    一.可视化方法 条形图 饼图 箱线图(箱型图) 气泡图 直方图 核密度估计(KDE)图 线面图 网络图 散点图 树状图 小提琴图 方形图 三维图 二.交互式工具 Ipython.Ipython not ...

  4. linux查看某个端口被哪个程序占用

    查看某个端口被哪个程序占用 netstat  -anp  |grep   端口号 查看进程号对应的程序 ps -ef | grep 17997 查看指定端口号的进程情况 netstat -tunlp

  5. Java并发(4):ThreadLocal

    一.对ThreadLocal的理解 ThreadLocal是java.lang包中的一个类,很多地方叫做线程本地变量,也有些地方叫做线程本地存储,其实意思差不多.可能很多朋友都知道ThreadLoca ...

  6. kubernetes1.9 手动安装

    一.创建TLS证书和秘钥: 1.安装 CFSSL: wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 chmod +x cfssl_linux-amd ...

  7. mysql数据库存储过程数据迁移案例与比较

    cursor 与 insert ...select 对比: cursor:安全,不会造成死锁,可以在服务运行阶段跑,比较稳定. insert...select :速度快,但是可能造成死锁,相比curs ...

  8. 子元素绝对定位absolute后,自动撑开宽度

    position: absolute;   white-space: nowrap;

  9. $git学习总结系列(4)——gitignore文件

    有时候工作区中会有我们创建的一些密码配置文件,或者自动生成的一些临时文件,比如python代码编译产生的.pyc文件和java代码编译产生的.class文件等,我们在提交代码的时候没有必要把这些文件也 ...

  10. JavaScript与Java数据类型的区别

    今天开始正式认真学习js,虽然在平常j2ee开发中也经常用到JS但并不精通,这次随笔记下js与Java数据类型的不同之处 Number 与java不同,js作为弱类型语言即使在浮点数与整数上也未作明确 ...