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. Angular学习笔记—创建一个angular项目

    开始项目前,你需要先安装node和npm,然后执行npm install -g @angular/cli安装Angular CLI. 如何安装node.js和npm npm使用介绍 1.安装angul ...

  2. ubuntu 配置tomcat 实测成功

    https://blog.csdn.net/qq_24091555/article/details/75077781

  3. c#的yield return

    4.1 迭代器块 一个迭代器块(iterator block)是一个能够产生有序的值序列的块.迭代器块和普通语句块的区别就是其中出现的一个或多个yield语句. yield return语句产生迭代的 ...

  4. python处理时间相关的方法

    记录python处理时间的模块:time模块.datetime模块和calendar模块. python版本:2.7 https://blog.csdn.net/songfreeman/article ...

  5. Apache 源码包安装

    系统:Centos 7.4 服务:Apache 2.4.33.apr 1.5.2.apr-util 1.5.4 依赖包: pcre.x86_64 pcre-devel.x86_64 openssl.x ...

  6. jQuery单选多选按钮选中美化特效

    在线演示 本地下载

  7. 分布式集群Session原理及实现共享

    1.什么是Session/Cookie? 用户使用网站的服务,基本上需要浏览器与Web服务器的多次交互.HTTP协议本身是无状态的,当用户的第一次访问请求结束后,后端服务器就无法知道下一次来访问的还是 ...

  8. 如何理解 Python 中的__init__

    转自https://www.zhihu.com/question/46973549/answer/103805810 定义类的时候,若是添加__init__方法,那么在创建类的实例的时候,实例会自动调 ...

  9. react native 之异步请求

    第一章 异步请求  fetch的运用 在react native  中异步请求一般用fetch这个方法, fetch的格式如下: const params ={ "charset" ...

  10. Spring_使用外部属性文件

    beans-properties.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns ...