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. Java swing: 实现ActionListener监听器的三种途径

    Swing是目前Java中不可缺少的窗口工具组,是用户建立图形化用户界面(GUI)程序的 强大工具.Java Swing组件自动产生各种事件来响应用户行为.如当用户点击按钮或选择菜单项目时,Swing ...

  2. linux crontab详解

    服务的启动和停止 cron服务是linux的内置服务,但它不会开机自动启动.可以用以下命令启动和停止服务: /sbin/service crond start /sbin/service crond ...

  3. 部署maria数据库到linux(源码编译安装)

    maria数据库是mysql原作者另外开发的一个版本,使用方法和mysql一样,可以直接用mysql的库连接. 在这下载包并解压: https://mariadb.org/download/ 建立数据 ...

  4. .net 各种序列化方式效率对比

    在服务与服务之间传输的是二进制数据,而在此之前有多种方法将数据内容进行序列化来减小数据传递大小,现针对于目前主流的几种序列化方式做了简单数据统计对比. 先做下简单介绍↓↓↓ 1.protobuf-ne ...

  5. idea启动tomcat报错:Error during artifact deployment. See server log for details.

    出现这种情况的原因老夫猜想是改变了artifact然而tomcat的配置中的artifact没有重新配就会出现这种报错 打开tomcat配置 删除原来的artifact 新添加artifact 保存 ...

  6. memcached readme

    memcache======== http://www.cnblogs.com/jeffwongishandsome/archive/2011/11/06/2238265.html # 命令 ## 存 ...

  7. easyui动态生成列

    需求:一个id对应多个key value 将id作为标识列 key值作为表头 value作为值显示.数据表可分为两张表 param数据表: 下表一个id对应上表多个key及value 如下图 id_p ...

  8. Java IO学习笔记(一)

    一.概念 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.在两设备间的传输的数据称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,以进行数据操作. 二.流分类 数据类型 ...

  9. EOutOfResources字符异常

    近日,用Delphi编程时,遇到一个莫名其妙的异常:EOutOfResources,这是一个可以重复再现的异常.开始以为是程序中创建的对象太多,导致占用了过多的资源,引起了这个异常.于是在代码中将许多 ...

  10. 洗礼灵魂,修炼python(5)--python操作符,内置函数

    前面提到了BIF(内置函数)这个概念,什么是内置函数,就是python已经定义好的函数,不需要人为再自己定义,直接拿来就可以用的函数,那么都有哪些BIF呢? 可以在交互式界面(IDLE)输入这段代码, ...