zoukankan      html  css  js  c++  java
  • 238. Product of Array Except Self

    题目来源:
     https://leetcode.com/problems/product-of-array-except-self/
    自我感觉难度/真实难度:

     这道题曾经在哪里做过,但是还是写不出代码,没想起来思路

    题意:

     有一个数组,每次等于除去自己的剩余数字之积

    可以利用排列组合,1              a1      a1a2      a1a2a3

                                 a4a3a2      a4a3      a4         1

    分析:
     
    自己的代码:
    class Solution:
        def productExceptSelf(self, nums: List[int]) -> List[int]:
            length=len(nums)
            templ=1
            tempr=1
            left=[]
            right=[]
            res=[]
            for i in nums:
                left.append(templ)
                templ*=i
                
            for j in range(-1,-length):
                tempr*=nums[j]
                res.append(tempr*left[j])
            return res.resvese()
    代码效率/结果:
     
    优秀代码:
    class Solution:
        def productExceptSelf(self, nums: List[int]) -> List[int]:
            length=len(nums)
            res=[]
            temp=1
            for i in nums:
                res.append(temp)
                temp*=i
            temp=1
            for j in range(length-1,-1,-1):
                res[j]=res[j]*temp
                temp=temp*nums[j]
            return res
    代码效率/结果:
     
    自己优化后的代码:
     
    反思改进策略:
    写题时间时长:

    56min

  • 相关阅读:
    第8章 字符串
    第7章 方法
    第6章 类和对象
    第5章 数组
    第4章 循环结构、break与continue
    第3章 选择结构
    第2章 变量、数据类型和运算符
    Codeforces Round #426 (Div. 2)
    CCNA学前基础一
    Tinkoff Challenge
  • 原文地址:https://www.cnblogs.com/captain-dl/p/10581978.html
Copyright © 2011-2022 走看看