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


题目标签:Array 

  题目给了我们一个 nums array, 让我们返回一个新的output array, 每一个项都是 整个数组除了自己项的乘积。题目规定了不能用除法,而且要O(n) 和 constant space complexity。

  来分析一下,每一个数字 都需要除了自己以外的 所有数的 乘积。 那么可以从另一个角度来看, 每一个数字, 需要自己位置 左边的所有数字 乘积 * 右边的所有数字 乘积。

  我们可以通过两次遍历数组来实现这个目标:

  首先把output[0] 设为1, 方便*;

  第一次遍历,从第二个数字到最后一个数字: 把output里每一个数字 = nums这个数字 左边 的所有数字的乘积;结束后可以发现,output里最后一个数字的 乘积 已经完成了,因为最后一个数字 没有右边的数字;

  第二次遍历,从倒数第二个数字到第一个数字, 并且设一个product = nums 里最后一个数字: 把output里每一个数字 * product,还要更新product = product * nums里数字,这一次遍历相当于把每一个数字的剩下右边所有数字乘积补上。

  举例来看一下:

  [2, 3, 4, 5]  nums

  [, 0, 0, 0]  output

  第一次遍历:

  [1, , 0, 0]

  [1, 2, , 0]

  [1, 2, 6, ]     结束第一次遍历;可以看出,第一次遍历同样有一个product 值,只不过直接保存在output里而已。

  第二次遍历:    product = 5;

  [1, 2, , 24]   更新product = 5 * 4;

  [1, , 30, 24]    更新product = 5 *4 * 3;

  [, 40, 30, 24]  结束。

    

Java Solution:

Runtime beats 29.29%

完成日期:09/07/2017

关键词:Array

关键点:来回遍历两次   第一次从左到右 -> 把每一个数字 等于 这个数字左边的所有数字的乘积;

             第二次从右到左 -> 把每一个数字 剩余的 右边所有数字乘积 补上

 class Solution
{
public int[] productExceptSelf(int[] nums)
{
// create output array
int [] output = new int[nums.length];
// make first number to 1
output[0] = 1; // 1st iteration from 1 ~ end -> make each nums[i] = 0 ~ i-1 numbers product
for(int i=1; i<nums.length; i++)
output[i] = nums[i-1] * output[i-1]; int product = nums[nums.length-1];
// 2nd iteration from end - 2 ~ 0 -> make each nums[i] * (i+1 ~ end) numbers product
for(int i=nums.length-2; i>=0; i--)
{
output[i] *= product;
product *= nums[i];
} return output;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

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. (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. 通过Excel认识POI

    1.POI是什么 Apache POI - the Java API for Microsoft Documents,顾名思义,Apache的三方包,用来操作微软office文档的,多数时候用来操作e ...

  2. C++代码使用 CppUnit 进行单元检测

    CppUnit是一个很方便的对C++代码进行单元检测的工具. 如何编译CppUnit参照博客:http://blog.csdn.net/x_iya/article/details/8433716 该博 ...

  3. NavigationController的返回按钮自定义

    假设需求时这样: NavigationController下有2个视图,从A视图会Push到B视图,默认情况下,当显示视图B时,视图B的导航bar上会出现返回按钮,按钮标题文字默认为A视图的title ...

  4. 第4章 同步控制 Synchronization ----Interlocked Variables

    同步机制的最简单类型是使用 interlocked 函数,对着标准的 32 位变量进行操作.这些函数并没有提供"等待"机能,它们只是保证对某个特定变量的存取操作是"一个一 ...

  5. JSP入门 el表达式

    我们已经知道el是jsp-2.0规范的一部分,tomcat-5.x版本以上都已经能够支持jsp-2.0规范,但在更低版本的tomcat和webphere,weblogic中还是无法使用这一便捷方式. ...

  6. Spring事务源码阅读笔记

    1. 背景 本文主要介绍Spring声明式事务的实现原理及源码.对一些工作中的案例与事务源码中的参数进行总结. 2. 基本概念 2.1 基本名词解释 名词 概念 PlatformTransaction ...

  7. LNMP环境源码搭建

    以前LNMP环境是由运维搭建,自己搭建的时候查找了很多资料,这是我见过的最棒的资料,将过程记录下来分享给大家 为啥使用LNMP而不是LAMP下面来谈谈Nginx的技能 Nginx是一个小巧而高效的Li ...

  8. 2013 ACM/ICPC Asia Regional Hangzhou Online hdu4739 Zhuge Liang's Mines

    Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. 我的第一个python web开发框架(4)——数据库结构设计与创建

    小白做好前端html设计后,马上开始进入数据库结构设计步骤. 在开始之前,小白回忆了一下老大在公司里培训时讲过的数据库设计解说: 对于初学者来说,很多拿到原型时不知道怎么设计数据表结构,这是很正常的事 ...

  10. HDU2282 Chocolate KM算法

    第一次做这样的题,其中有几个细节是反复思考反复调试,最后一A的,ORZ,又加深了对KM算法的理解.能不参考网上的题解,而是平静下来思考,参透,最后敢于尝试.....真的很重要,以后遇到才会有更深的印象 ...