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. layer 如何加上关闭框

    layer 如何加上关闭框

  2. JAVA调用WebService接口(以调用第三方天气接口为例)

    天气接口地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 1.打开项目文件目录路径文件夹,在当前文件夹打开cmd, ...

  3. Spring Boot整合Thymeleaf及Thymeleaf页面基本语法

    引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  4. 【LeetCode】702. Search in a Sorted Array of Unknown Size 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 二分查找 日期 题目地址:https://lee ...

  5. 【经验】基于阿里云 Ubuntu 的 LAMP 网站搭建及配置完全教程

    本文同步发表在负雪明烛的博客:https://fuxuemingzhu.cn/2016/03/02/My-Aliyun-Server-Setting/ 起因 最近老师让我做一个众筹系统,可以在微信公众 ...

  6. 【LeetCode】293. Flip Game 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  7. 【LeetCode】941. Valid Mountain Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】225. Implement Stack using Queues 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. spoj-SUBSUMS - Subset Sums

    SUBSUMS - Subset Sums Given a sequence of N (1 ≤ N ≤ 34) numbers S1, ..., SN (-20,000,000 ≤ Si ≤ 20, ...

  10. Monkey工具之fastbot-iOS实践

    Monkey工具之fastbot-iOS实践 背景 目前移动端App上线后 crash 率比较高, 尤其在iOS端.我们需要一款Monkey工具测试App的稳定性,更早的发现crash问题并修复. 去 ...