class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
res=[0]*len(nums)
res[0]=1
for i in range(1,len(nums)):
res[i]=res[i-1]*nums[i-1]
r=1
for i in range(len(nums)-1,-1,-1):
res[i]=res[i]*r
r=r*nums[i]
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
res=[0]*len(nums)
res[0]=1
for i in range(1,len(nums)):
res[i]=res[i-1]*nums[i-1]
r=1
for i in range(len(nums)-1,-1,-1):
res[i]=res[i]*r
r=r*nums[i]
return res
解题思路:
先从左到右循环的时候把每一位用左边的乘积记录:第一位用1记录
再从右到左循环,每一位再乘以右边的乘积
Python中从大到小循环时到-1才停止