Leetcode #238 Product of Array Except Self
2015-09-02 22:03
330 查看
Given an array of n integers where n > 1,
return an array
equal to the product of all the elements of
Solve it without division and in O(n).
For example, given
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.)
Difficulty:Medium
注意0的情况
nums,
return an array
outputsuch that
output[i]is
equal to the product of all the elements of
numsexcept
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.)
Difficulty:Medium
注意0的情况
class Solution(object): def productExceptSelf(self, nums): """ :type nums: List[int] :rtype: List[int] """ maxans = 1 zeroNum = 0 ans = [] for i in nums: if i==0: zeroNum+=1 else: maxans*=i if zeroNum==0: for i in nums: ans.append(maxans/i) elif zeroNum==1: for i in nums: if i==0: ans.append(maxans) else: ans.append(0) else: for i in nums: ans.append(0) return ans
相关文章推荐
- QT 多线程程序设计
- Linux命令备忘实例(3)——数学运算
- 亚马逊与微软赢得一亿美元云计算合约,IBM黯然神伤
- 什么样的项目适合自动化测试
- 数据库校招笔试题(1)
- 树状数组求区间最大值
- Python游戏服务器开发日记(五)skynet_messagequeue和skynet_timer
- UIActivityIndicatorView活动指示条
- OC04_属性
- LeetCode 9_Palindrome Number
- lintcode-子树
- poj1860 Currency Exchange (bellman_ford)
- Java字符流与字节流的区别
- python做全文检索引擎
- SQL server 数据库远程连接
- poj 3613 矩阵快速幂变形(恰好k条边最短路)
- 静态查找表
- 【bzoj3680】【吊打XXX】【模拟退火】
- (JavaScript)案例二:多条件查询
- scp的具体用法