边工作边刷题:70天一遍leetcode: day 61-3

Product of Array Except Self

要点:和Candy之类的都是一个路数,注意这题已经限定了n>1,所以不用考虑n<=1的边界条件

class Solution(object):
    def productExceptSelf(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        n = len(nums)
        res = [0]*n
        res[0]=1
        for i in xrange(1, n):
            res[i]=res[i-1]*nums[i-1]
        
        right = 1
        for i in xrange(n-1,-1,-1):
            res[i]=res[i]*right
            right*=nums[i]
        
        return res
posted @ 2016-06-12 05:04  absolute100  阅读(60)  评论(0编辑  收藏  举报