zoukankan      html  css  js  c++  java
  • leetcode-hard-array-238. Product of Array Except Self-NO

    mycode   99.47%

    class Solution(object): 
        def productExceptSelf(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            temp = 1
            res = list()
            res.append(1)
            #nums的第一个数和最后一个数是最特殊的,因为别人都是左右可以包围的,即product=该数前面所有数的乘积*该数后面所有数的乘积
            #所以res[0]放1,代表nums[0]前面的数(其实没有)的product
            for i in range(len(nums)-1):
            #当i为len(nums)-2时,计算出来的乘积就是nums最后一个元素的product结果
                temp *= nums[i]
                res.append(temp) #res[1]放的时nums[1]前面的数的product
            #i==len(nums)-2时,res[len(nums)-2]放的就是nums[-1]前面的数的product,而nums[-1]后面是没有数的,所有逆向遍历的时候只需要从nums[len(nums)-2]开始计算它后面的乘积
            temp = 1
            for i in range(len(nums)-2,-1,-1):
                temp *= nums[i+1]
                res[i] = res[i]*temp
            return res
  • 相关阅读:
    192021
    191020
    magento注册
    magento登陆
    把PHP的数组变成带单引号的字符串
    magento直接操作数据库
    兼容各大浏览器的event获取
    手动修改magento域名
    微信支付中的jsapi返回提示信息
    CentOS 下安装xdebug
  • 原文地址:https://www.cnblogs.com/rosyYY/p/11038510.html
Copyright © 2011-2022 走看看