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. sublime text3 好用的插件!!!

    1.首先,你要保证sublime有Package Control,所以,如果没有,那么将Ctrl+`打开sublime控制台,将下列代码复制进去! import urllib.request,os; ...

  2. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a

    如果出现了这个错误,看看在使用Hibernate的时候有没有在事务的环境下执行操作!

  3. 过滤器复用代码【中文乱码、HTML转义】

    中文乱码 public class CharacterEncodingFilter implements Filter { public void doFilter(ServletRequest re ...

  4. Html在线编辑器--基于Jquery的xhEditor轻量级编辑器

    xhEditor V1.2.2 下载地址 开源中国社区: http://www.oschina.net/p/xheditor xhEditor是一个基于jQuery开发的简单迷你并且高效的可视化XHT ...

  5. Python学习笔记004_字典_集合

    >>> # 字典 用大括号表示, 它是影射类型,相当于java中的Map >>> >>> dict1 = {'李宁': '一切皆有可能', '耐克 ...

  6. python import xxx 与 from xxx import xx 模块引入的区别

    有如下脚本script1.py: A='aaaa'B='bbbb'C='cccc'print A,B,C 1.命令行交互模式下使用import 导入方式1: >>>import sc ...

  7. angular directive知识

    一般来讲 directive名字遵循一下规则: 1.忽略以x-和data-为元素/属性的前缀 2.转化“:”,“-”,“_”命名为驼峰命名 如下所示 <div ng-controller=&qu ...

  8. NET应用——使用RSA构建相对安全的数据交互

    最近又被[现场破解共享单车系统]刷了一脸,不得不开始后怕:如何防止类似的情况发生? 想来想去,始终觉得将程序加密是最简单的做法.但是摩拜.ofo也有加密,为什么仍然被破解?那是因为请求在传输过程中被篡 ...

  9. ArrayListd的长度问题

    namespace ArrayListd的长度问题{ class Program { static void Main(string[] args) { //需要的参数是object类型 //alt+ ...

  10. JavaScript简易计算器

    JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标 ...