238 Product of Array Except Self

238 Product of Array Except Self 

从头部 和 尾部各扫描一遍

class Solution:
    # @param {integer[]} nums
    # @return {integer[]}
    def productExceptSelf(self, nums):
        ans = [1]
        for i in range(0, len(nums)-1):
            ans.append(ans[-1]*nums[i])
        tmp = 1
        for i in range(len(nums)-1,-1,-1):
            ans[i] *= tmp
            tmp *= nums[i]
        return ans

 

posted @ 2015-07-17 07:29  dapanshe  阅读(108)  评论(0编辑  收藏  举报