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 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 (去除自己的数组之积)的更多相关文章
- 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 ...
- 剑指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] * ...
- [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 ...
- (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 ...
- 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] ...
- 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 ...
- [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 ...
- leetcode 238 Product of Array Except Self
这题看似简单,不过两个要求很有意思: 1.不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨. 2.空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里 ...
- Leetcode 238 Product of Array Except Self 时间O(n)和空间O(1)解法
1. 问题描写叙述 给定一个n个整数的数组(n>1n>1)nums,返回一个数组output,当中的元素outputioutput_i的值为原数组nums中除numsinums_i之外的全 ...
随机推荐
- Python的自学之路:Python基础(一)
声明:我写博客不是为了什么,只是为了记录自己的学习状态,学过的知识点!方便以后进行好的复习!python小白,勿喷 python环境的搭建,在这里就不细说了,这里有我的链接,可以参考一下:https: ...
- linux各类压缩解压命令大全
01-.tar格式解包:[*******]$ tar xvf FileName.tar 打包:[*******]$ tar cvf FileName.tar DirName(注:tar是打包,不是压缩 ...
- Mybatis第八篇【一级缓存、二级缓存、与ehcache整合】
Mybatis缓存 缓存的意义 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题. myba ...
- birt IE8 IE9 兼容问题
我自己的应用,birt展示报表的时候,在firefox和IE下显示的样式: IE8下的: Firefox下的: 我的项目,应用birt部分的结构如下: 1, prototype.js ,查找下面这段代 ...
- Linux配置SSH端口以及密钥登录
改端口后重启: vim /etc/ssh/sshd_config systemctl restart sshd
- iOS蓝牙心得
1.获取蓝牙mac地址 因为安卓不能得到uuid,所以,在要同步的时候要将uuid转换成mac地址,下面是转换方法 [peripheral discoverServices:@[[CBUUID UUI ...
- Android中Parcelable接口
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- MMORPG战斗系统随笔(二)、浅谈场寻路Flow Field PathFinding算法
转载请标明出处http://www.cnblogs.com/zblade/ 今天给大家带来一篇游戏中寻路算法的博客.去年,我加入一款RTS的游戏项目,负责开发其中的战斗系统,战斗系统的相关知识,属于游 ...
- 针对Openlayer3官网例子的简介
网址:http://openlayers.org/en/latest/examples/ 如果大家想了解ol3能做什么,或者说已提供的API有什么,又闲一个个翻例子跟API累的话,就看看这个吧. 1. ...
- vue数组语法兼容问题
先来一行代码: <a :href="['NewsNote.asp?ID='+item.ID+'&MenuType=C']" v-text="item.Tit ...