lc 238 Product of Array Except Self


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

Follow up:

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

Accepted

本来可以先求出所有元素的积,再以O(n)的复杂度遍历除得答案,但题目要求不能使用除法。

我们知道对于答案的第一位一定等于从后往前乘,直至乘到它为止,不包括它自身;而答案的最后一位一定等于从前往后乘,直至乘到它为止,不包括它自身。所以我们定义两个变量,frombegin代表从前往后的积,fromlast代表从后往前的积。

通过一层for循环,对于每一次迭代都对当前元素乘上frombegin,对其对称位乘上fromlast,这样最终就可以很神奇地得到除去自身外的其他位的乘积。

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

LN : leetcode 238 Product of Array Except Self的更多相关文章

  1. 剑指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] * ...

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

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

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

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

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

  8. leetcode 238 Product of Array Except Self

    这题看似简单,不过两个要求很有意思: 1.不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨. 2.空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里 ...

  9. Leetcode 238 Product of Array Except Self 时间O(n)和空间O(1)解法

    1. 问题描写叙述 给定一个n个整数的数组(n>1n>1)nums,返回一个数组output,当中的元素outputioutput_i的值为原数组nums中除numsinums_i之外的全 ...

随机推荐

  1. 权限框架之Shiro详解(非原创)

    文章大纲 一.权限框架介绍二.Shiro基础介绍三.Spring Boot整合Shiro代码实战四.项目源码与资料下载五.参考文章   一.权限框架介绍 1. 什么是权限管理   权限管理属于系统安全 ...

  2. myloader恢复mysql数据库演示样例

     mydumper是针对mysql数据库备份的一个轻量级第三方的开源工具.备份方式为逻辑备份.它支持多线程.备份速度远高于原生态的mysqldump以及众多优异特性.与其相配套的恢复工具则是mylo ...

  3. list.ensureCapacity竟然会变慢

    list.ensureCapacity竟然会变慢 jdk1.8 应该是做了优化了: public class Test10 { public static void main(String[] arg ...

  4. LeetCode 之 Longest Valid Parentheses(栈)

    [问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...

  5. [Javascript] Flattening nested arrays: a little exercise in functional refactoring

    In this lesson we write an imperative function to flatten nested arrays, and then use the popular ma ...

  6. powershell 的版本号所引起的载入 FSharp 编译器问题

    powershell 的版本号所引起的载入 FSharp 编译器问题 在 64 位的系统下,大部分系统文件都有 64 位和 32 位的版本号:通常在C:\WINDOWS\system32 下的是 64 ...

  7. docker (1) ---简介,使用

    一.docker简介: 容器( container-based )虚拟化方案,充分利用了操作系统本身已有的机 制和特性,以实现轻量级的虚拟化(每个虚拟机安装的不是完整的虚拟机), 甚至有人把他称为新一 ...

  8. C Language Study - gets , getchar &amp; scanf

    慢慢的发现C语言功底是如此的薄弱,被这几个字符输入函数搞糊涂了又~~ 来,再来忧伤一次吧~ 那么.我们从scanf開始: 假如说你要将一串字符输入到一字符数组里,例如以下面程序, char a[2]; ...

  9. 第一天,Robert和Sue大师培训给的启示

    程序猿的零点从他睡觉那一刻开始计时. 今天是周六,听到了Robert关于销售技巧的培训还有Sue关于微信零售业O2O电商的分析,一并加上昨天晚上直到11点的Leadership培训,这个周末真的是收获 ...

  10. #pragma pack (n) 惹的祸

    今天遇到了一个问题,使用数据流传输的数据在解析的时候数据错位.想了非常久,发现是#pragma pack (n)惹的祸. 首先.解析方使用了编译字节设置,可是在发送方没有使用,于是用相同的结构体解析数 ...