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. mysql聚合函数操作

    1.mysql对中文进行排序 注:是用convert函数用gb2312编码转换 SELECT * FROM 表名 ORDER BY CONVERT(字段名 USING gb2312 ) ASC;

  2. 并行求pi (C++实现)

    用OpenMP并行化求pi的代码,这里用的是公式法求pi.具体如下: //公式法 #include<omp.h> #include<stdio.h> #include<s ...

  3. linux命令(6/10):find 命令

    Linux将时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟两种.系统时间是指当前Linux Kernel中的时钟, 而硬件时钟则是主板上由电池供电 ...

  4. 优秀 H5 案例收集 vol.3(不定期更新)

    上期浏览:Vol.1   Vol.2 爱的不同定义,五笔连成爱http://news.163.com/special/fdh5_valentines/ 世界华语悬疑文学大赛—下一位悬疑大师,就是你!h ...

  5. Spring 之定义切面尝试(基于 XML)

    有些场景下只能基于 XML 来定义切面. [Spring 之定义切面尝试] 1.XML 下定义切面(首先是要有一个对应的类...显然要比基于注解的麻烦) <?xml version=" ...

  6. Django 函数和方法的区别

    函数和方法的区别 1.函数要手动传self,方法不用传 2.如果是一个函数,用类名去调用,如果是一个方法,用对象去调用 class Foo(object): def __init__(self): s ...

  7. Centos 7 Sublime 安装 package control

    1. 修改installed Packages 下的package Control.sublime-package(原文件是个空的文件) 2. 下载channel_v3.json 可放在任何位置并在s ...

  8. QMesageBox的使用

    一.使用构造函数弹出对话框 1. QMessageBox msgBox://最简单的对话框,里面什么也没有 QString str = “test”: msgBox.setText(str); msg ...

  9. nswag vs swashbuckle

    https://www.reddit.com/r/dotnet/comments/a2181x/swashbuckle_vs_nswag/ Swashbuckle https://github.com ...

  10. oracle 10g和11g将表到缓存到内存中

    alter table 表名 cache;alter table 表名 storage(buffer_pool keep);