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之外的全 ...
随机推荐
- 【译】The Accidental DBA:Troubleshooting Performance
最近重新翻看The Accidental DBA,将Troubleshooting Performance部分稍作整理,方便以后查阅.此篇是Part 2Part 1:The Accidental DB ...
- oracle "记录被另一个用户锁定"
出现的原因是有人对某一条数据进行了修改,oracle会通过这个事务记住这条数据,若修改的人没有进行提交或进行回滚记录,oracle是不允许对这条数据在此进行修改的,在这种情况下你要进行修改数据,则会被 ...
- mongodb 在windows下面进行副本建创建
一:主从复制 1: 首先看看模型图 2: 从上面的图形中我们可以分析出这种架构有如下的好处: <1> 数据备份. <2> 数据恢复. <3> 读写分离. 3: ...
- 实现一个简单的虚拟DOM
现在的流行框架,无论React还是Vue,都采用虚拟DOM. 好处就是,当我们数据变化时,无需像Backbone那样整体重新渲染,而是局部刷新变化部分,如下组件模版: <ul class=&qu ...
- 使用Pano2VR 切割图片
图片转换好之后得到一组立方体面片.
- php多态
多态性是指相同的操作或函数.过程可作用于多种类型的对象上并获得不同的结果.不同的对象,收到同一消息将可以产生不同的结果,这种现象称为多态性. 多态性允许每个对象以适合自身的方式去响应共同的消息.多态性 ...
- CentOS服务器上的 git 包版本控制
本文衔接上文"记录一次无聊的(经历了Nodejs -> Shell -> C)的探索问题过程",服务器上git版本是1.8.3.1,使用的pm2来管理nodejs进程, ...
- swift 开发学习问题
1 UITableViewController 问题: [UITableView dequeueReusableCellWithIdentifier:forIndexPath:], unable to ...
- ApplicationContextAware
1.实现了ApplicationContextAware接口,在Bean的实例化时会自动调用setApplicationContext()方法! 2.通过调用静态方法getBean即可获取 sprin ...
- 自测-5 Shuffling Machine
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...