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. 应用CLR的线程池

    大家都知道这个线程的建立和销毁都需要很大的性能开销,当有比较多且不同的任务需要完成时,可以考虑使用线程池来管理这些线程.在以windows NT为内核的操作系统上每个进程都包含一个线程池,在线程池中存 ...

  2. SHARP AR-2048D/2348D

    http://www.sharp.cn/printer/AR-2048D%7C2348D/support/download.html

  3. 爱普生L201

    http://tech.sina.com.cn/b/2011-03-29/05481698131.shtml

  4. Cocos2d-x v3.1.1 创建以及编译项目

    1.安装python, 并将安装路径增加系统环境变量中; 2. 执行cocos2d-x根文件夹下的setup.py; 3. 进入cmd, 输入: cocos new 项目名称 -p 包名 -l 语言类 ...

  5. 搭建ELK收集PHP的日志

    架构: filebeat --> redis -->logstash --> es --> kibana 每个客户端需要安装filebeat收集PHP日志 filebeat把收 ...

  6. 012 router password

    Press RETURN to get started!       Router>en Router#config t Enter configuration commands, one pe ...

  7. VM虚拟机的网卡模式介绍

    (1)Bridged方式 用这种方式,虚拟系统的IP可设置成与本机系统在同一网段,虚拟系统相当于网络内的一台.独立的机器,与本机共同插在一个Hub上,网络内其他机器可访问虚拟系统,虚拟系统也可访问网络 ...

  8. Unity3D 玻璃 Shader

     Shader "Custom/Glass" { // Upgrade NOTE: replaced 'SeperateSpecular' with 'SeparateSpec ...

  9. C++获取时间的方法

    //方案- 长处:仅使用C标准库:缺点:仅仅能精确到秒级 #include <time.h>  #include <stdio.h>  int main( void )  {  ...

  10. Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game 数学题

                                                     C. Vasya and Petya's Game                           ...