【LeetCode】Product of Array Except Self
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.)
Solution:
题目中说不要用到 division 并且要在线性时间内得出结果。
首先受上一题影响想想能不能位运算,但这题乘法是不大行的。
那么要如果扫描一遍就得出要求的Array呢?
想想我们遍历 Array 时我们可以干些什么,我们可以对当前秩所在位置进行操作,关键是我们还可以对当前秩前后常数位置也可以进行操作。
考虑当前遍历到的秩 i,我们可以把得出 ans[i] 的过程分为两部分:一部分是对于所有小于 i 的秩的元素的相乘,另一部分则是所有大于 i 的秩的元素的相乘。
独立地来完成这两部分并不难,只要在遍历一遍时,用一个变量记录已经遍历到的元素的乘积(不包括当前元素),乘到 ans[i]即可。
而这两个独立的部分其实也可以合成一个遍历来完成,因为只要知道了整个需要遍历的Array的长度,从左向右和从右向左只是一个取模的问题了。
代码如下:
class Solution:
# @param {integer[]} nums
# @return {integer[]}
def productExceptSelf(self, nums):
n = len(nums)
ans = [1] * n
left_fac, right_fac = 1, 1
for i in range(n):
ans[i] *= left_fac
left_fac *= nums[i]
ans[n - i - 1] *= right_fac
right_fac *= nums[n - i - 1]
return ans
【LeetCode】Product of Array Except Self的更多相关文章
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- 【leetcode81】Product of Array Except Self
题目描述: 给定一个长度为n的整数数组Array[],输出一个等长的数组result[],这个输出数组,对应位置i是除了Array[i]之外,其他的所有元素的乘积 例如: given [1,2,3,4 ...
- 【08_238】Product of Array Except Self
Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...
- 【数组】Product of Array Except Self
题目: iven an array of n integers where n > 1, nums, return an array output such that output[i] is ...
- 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...
- 【LeetCode】932. Beautiful Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...
- 【LeetCode】922. Sort Array By Parity II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...
随机推荐
- Emacs下的中文输入
Emacs如此优秀的编辑器,如果输入中文不顺畅,不免遗憾.可惜现实是折腾很久也未必用得称心如意,作为一个重度(也许是中毒) Emacs使用者,根据个人经验写下此文,希望对同道中人有所帮助. 在Wind ...
- 本地搭建PHP环境后进入应用失败
进入localhost/wordpress或者其他应用,直接出现类似以下这种情况: 解决方法是修改tomcat的配置文件httpd.conf中: <IfModule dir_module> ...
- Sublime Text 3 使用问题答疑
命令面板/命令模式:ctrl+shift+pctrl+cctrl+v → ctrl+shift+v粘贴时会保持原格式(缩进)ctrl+sctrl+z撤销ctrl+y恢复撤销在当前行下面添加一行:ctr ...
- 解析Hibernate中的持久化—ORM(转载)
最近一直在学习Hibernate,首先说一下Hibernate出现的原因吧,Hibernate是因为MVC的分层体系结构的出现,即数据持久层(模型层)的出现,持久层是在MVC三层架构的基础上提出来的, ...
- Topcoder SRM558 1000 SurroundingGame
题意:给定一个网格,每个网格有选取代价和占据收益.每个点被占据,需要满足以下两个条件至少一个条件:1.被选取 2.邻近方格都被选取(有公共边被称为邻近) 不一定要占据所有方格,求最大收益. 第一直 ...
- C++11 变长模版和完美转发实例代码
C++11 变长模版和完美转发实例代码 #include <memory>#include <iostream>#include <vector>#include ...
- HTML 中级
abbr(表示它所包含的文本是一个更长的单词或短语的缩写形式): <p>This web site is about <abbr title="HyperText Mark ...
- java.net.UnknownHostException: Unable to resolve host "api102.meishi.cc": No address associated with hostname
有三个原因: 清单文件Mei加网络访问权限 在主线程中执行了耗操作 你没开wifi
- 抛弃msvcrtXX库
对于极致要求体积的程序来说.抛弃Msvcrt里的函数是必要的.(尤其是msvcrtXX库)因为要使用mscvrt中的函数,就需要带上相对来 说,不能容忍的几kb的,vcrt初始化函数,包围在我们的Wi ...
- hdu 1082, stack emulation, and how to remove redundancy 分类: hdoj 2015-07-16 02:24 86人阅读 评论(0) 收藏
use fgets, and remove the potential '\n' in the string's last postion. (main point) remove redundanc ...