1. Product of Array Except Self

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

Example:

Input:  [1,2,3,4]
Output: [24,12,8,6]

Constraint: It's guaranteed that the product of the elements of any prefix or suffix of the array (including the whole array) fits in a 32 bit integer.

Note: Please solve it without division and in O(n).

Follow up:

Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)

left-right。计算第i个数左边和右边的乘积

class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int>ans;
vector<int>left(nums.size(), 1), right(nums.size(), 1);
for(int i = 1; i < nums.size(); ++i){
left[i] = left[i-1] * nums[i-1];
}
for(int i = nums.size()-1; i > 0; --i){
right[i-1] = right[i]*nums[i];
}
for(int i = 0; i < nums.size(); ++i){
ans.push_back(left[i]*right[i]);
}
return ans;
}
};

优化:在从右往左扫描时,right数组中的数字只用了一次,因此用一个变量R代替即可

class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int>ans(nums.size(), 1);
for(int i = 1; i < nums.size(); ++i){
ans[i] = ans[i-1] * nums[i-1];
}
int R = 1;
for(int i = nums.size()-1; i >= 0; --i){
ans[i] *= R;
R *= nums[i];
}
return ans;
}
};

【刷题-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):题目说明了返回的那个数组不算进复杂度分析里 ...

  10. [leetcode] 238. Product of Array Except Self (medium)

    原题 思路: 注意时间复杂度,分别乘积左右两边,可达到O(n) class Solution { public: vector<int> productExceptSelf(vector& ...

随机推荐

  1. CF760A Petr and a calendar 题解

    Content 输入两个数 \(m,d\),请输出 \(2017\) 年 \(m\) 月的日历[其中第一天是星期 \(d\)(如果 \(d=7\) 就是星期天)]需要印的列数. 格式见题目所述. 数据 ...

  2. java 、abstract修饰的【抽象类】【比如几何图形类】

    现实中问题引入 现实中一类具有共同特征的类,但是无法具体实现.,比如我们定义了一个几何类,叫做Shape,我们有一个方法要计算周长,直接在每个子类实现虽然可以,但是无法通过Shape定义的实例来访问到 ...

  3. 音视频中的PTS和DTS及同步

    视频的播放过程可以简单理解为一帧一帧的画面按照时间顺序呈现出来的过程,就像在一个本子的每一页画上画,然后快速翻动的感觉.       但是在实际应用中,并不是每一帧都是完整的画面,因为如果每一帧画面都 ...

  4. libevent源码学习(11):超时管理之min_heap

    目录min_heap的定义向min_heap中添加eventmin_heap中event的激活以下源码均基于libevent-2.0.21-stable.       在前文中,分析了小顶堆min_h ...

  5. Various methods for capturing the screen

    Explains techniques for capturing the screen programmatically. Download GDI source code - 72.1 Kb Do ...

  6. Xshell连接Ubuntu服务器连接不上 显示拒绝了密码

    确保下方两个都安装了 sudo apt-get install openssh-server sudo apt-get install ssh 修改 vim /etc/ssh/sshd_config ...

  7. Linux执行脚本报错:-bash: ./xx.sh: /bin/bash^M: bad interpreter: No such file or directory

    1.用vim打开文本 输入 : set ff 这里要先按":"号 显示文件为dos格式 2.强制装换格式为unix 先按冒号":" set ff=unix 然后 ...

  8. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  9. RabbitMQ学习笔记六:RabbitMQ之消息确认

    使用消息队列,必须要考虑的问题就是生产者消息发送失败和消费者消息处理失败,这两种情况怎么处理. 生产者发送消息,成功,则确认消息发送成功;失败,则返回消息发送失败信息,再做处理. 消费者处理消息,成功 ...

  10. BAIRE ONE FUNCTIONS (Baire第一类函数)

    目录 定义 导函数 一致收敛性质 的连续点 JOHNNY HU, BAIRE ONE FUNCTIONS. 一些基本的定义(诸如逐点收敛, 一致收敛\(F_{\sigma}\)集合等)就不叙述了. 定 ...