Product of Array Except Self
2015-08-26 19:24
393 查看
题目:
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.)
解题思路:
思路1(不合题目限制,但是AC):
考虑三种情况:(1)当所有元素均为非0数时... (2)当有一个元素为0时... (2)当有多于一个元素为0时...
class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
ans = []
sum_all, numOfzero = 1,0
for v in nums:
if v!=0:
sum_all *= v
else:
numOfzero += 1
if numOfzero>1:
return [0 for i in range(len(nums))]
elif numOfzero==1:
for v in nums:
if v!=0:
ans.append(0)
else:
ans.append(sum_all)
else:
for v in nums:
ans.append(sum_all/v)
return ans
思路二:
我们可以申请一个等大的数组,第一个记录对应第一个元素的前面数字乘积,很显然是1(因为当前数字不算,而之前又没有数字,只能是1);往后递推,第二个记录对应除第二个元素之前的所有数字乘积,即需要刚才的1乘以刚才的第一个数字;形成数组left。从左往右遍历完成之后,left中对应数字的结果是所有左侧数字的乘积;那再从右往左便利一遍,再记录对应数字右侧所有数字的乘积,形成right。最终把两个数乘起来,不就得到想要的结果了吗。
另外,从右往左乘的过程,其实没有必要再申请一个新的数组了,只需要申请个别变量记录当前值,在刚才的数组上做即可。因为,当前值只跟上一个值有关。
通过一左一右两次扫描,就能够得到所求结果了。
class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
res = [0 for i in nums]
for i,v in enumerate(nums):
if i==0:
res[i] = 1
continue
res[i] = res[i-1]*nums[i-1]
j = len(res)-2
n = nums[j+1]
while j>=0:
res[j] = res[j]*n
j = j-1
n = n*nums[j+1]
return res
Given an array of n integers where n > 1,
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.)
解题思路:
思路1(不合题目限制,但是AC):
考虑三种情况:(1)当所有元素均为非0数时... (2)当有一个元素为0时... (2)当有多于一个元素为0时...
class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
ans = []
sum_all, numOfzero = 1,0
for v in nums:
if v!=0:
sum_all *= v
else:
numOfzero += 1
if numOfzero>1:
return [0 for i in range(len(nums))]
elif numOfzero==1:
for v in nums:
if v!=0:
ans.append(0)
else:
ans.append(sum_all)
else:
for v in nums:
ans.append(sum_all/v)
return ans
思路二:
我们可以申请一个等大的数组,第一个记录对应第一个元素的前面数字乘积,很显然是1(因为当前数字不算,而之前又没有数字,只能是1);往后递推,第二个记录对应除第二个元素之前的所有数字乘积,即需要刚才的1乘以刚才的第一个数字;形成数组left。从左往右遍历完成之后,left中对应数字的结果是所有左侧数字的乘积;那再从右往左便利一遍,再记录对应数字右侧所有数字的乘积,形成right。最终把两个数乘起来,不就得到想要的结果了吗。
另外,从右往左乘的过程,其实没有必要再申请一个新的数组了,只需要申请个别变量记录当前值,在刚才的数组上做即可。因为,当前值只跟上一个值有关。
通过一左一右两次扫描,就能够得到所求结果了。
class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
res = [0 for i in nums]
for i,v in enumerate(nums):
if i==0:
res[i] = 1
continue
res[i] = res[i-1]*nums[i-1]
j = len(res)-2
n = nums[j+1]
while j>=0:
res[j] = res[j]*n
j = j-1
n = n*nums[j+1]
return res
相关文章推荐
- A,B两个整数集合,设计一个算法求他们的交集,尽可能的高效(牛客网)
- c/c++ 取得当前目录
- Ajax提高篇(2)DOM进行Web响应
- WebViewer报错Error loading document: Invalid XOD file: Zip end header data is wrong size!
- [POJ2503]Babelfish
- oracle ORA-12560: TNS: 协议适配器错误的问题
- ubutun安装unoconv转换office文件为PDF
- C语言基础知识之(八):二维数组,二维字符串数组,代码安全
- Python多进程编程
- android自动化工具之--Monkeyrunner使用方法及实例
- C++中的error C2662,const的this指针问题
- (EM算法)The EM Algorithm
- PHP底层的运行机制与原理
- android自动化工具之--初体验之Monkey和MonkeyRunner
- 13、Apache中虚拟目录和目录权限配置
- uvalive 4975
- Gradle与Makefile构建工具的对比
- Gradle与Makefile构建工具的对比
- Sqlite第三方类库:FMDB使用
- 关于const的底层实现