2021-09-20:除自己的乘积,random pick with weight,二叉树的直径,加法器 add string

1617-卿同学

发表文章数:84

热门标签

首页 » MySQL » 正文

2021-09-20:除自己的乘积,random pick with weight,二叉树的直径,加法器 add string

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        k = 1
        n = len(nums)
        res = [1] * n
        for i in range(n):
            res[i] = k
            k = k*nums[i]
        k = 1
        for i in range(n-1,-1,-1):
            res[i] = res[i]*k
            k = k*nums[i]
        return res

(1) 随机的浮点数,范围是在0.0~1.0之间:random.random();
  (2) 函数随机生成一个[a,b]范围内的浮点数:random.uniform(a, b);
  (3) 随机生成一个范围[a, b]内的整数:random.randint(a, b);

  (4) 随机选取一个元素返回或随机生成字符:random.choice();
  (5) 随机打乱元素:random.shuffle();
  (6) 从序列a中截取指定长度n的片段:random.sample(a, n);
  (7) 随机选取a到b间的奇数1/偶数2:random.randrange(a, b, 2)。

2021-09-20:除自己的乘积,random pick with weight,二叉树的直径,加法器 add string

前缀和: random

2021-09-20:除自己的乘积,random pick with weight,二叉树的直径,加法器 add string

class Solution:

    def __init__(self, w: List[int]):
        self.prefixsum = [0]*len(w)
        self.prefixsum[0] = w[0]
        for i in range(1,len(w)):
            self.prefixsum[i] = self.prefixsum[i-1] +w[i]
            
        print(self.prefixsum)
    def pickIndex(self) -> int:
        seed = random.randint(1,self.prefixsum[-1])
        return  bisect_left(self.prefixsum, seed)
        

# Your Solution object will be instantiated and called as such:
# obj = Solution(w)
# param_1 = obj.pickIndex()

2021-09-20:除自己的乘积,random pick with weight,二叉树的直径,加法器 add string

class Solution:
    def addStrings(self, num1: str, num2: str) -> str:
        i = len(num1)  # 1
        j = len(num2)  # 2
        carry = 0
        res = ""
        while i>0 or j>0:
            n1 = int(num1[i-1]) if i>0 else 0  # n1 = 1
            n2 = int(num2[j-1]) if j>0 else 0  # n2 = 3
            temp  = n1+ n2 +carry   # temp = 4
            res = str(temp % 10) + res  #  4
            carry = temp // 10  # 0
            i -= 1  # 0
            j -= 1  # 1
        return '1'+res if carry else res

2021-09-20:除自己的乘积,random pick with weight,二叉树的直径,加法器 add string

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        self.result = 0
        self.helper(root)
        return self.result
    def helper(self, root):
        if not root.left and not root.right:
            return 0
        left = self.helper(root.left) if root.left else 0
        right = self.helper(root.right) if root.right else 0
        print(left,right)
        path = 0
        path += left +1 if root.left else 0
        path += right+1 if root.right else 0
        self. result = max(self.result, path)
        return max(left,right) + 1 
    
    
    
    '''
    dfs 4:  left = 0 right = 0 helper(4) = 0
    dfs 5: left = 0 right = 0 helper(5) = 0
    dfs 2: left = 0 right = 0 helper(2) = 1 result = 2
    dfs 3  helper(3) = 0
    helper(1)=2 left = 1 right = 0 result = 1+0+1+1 =3
    
    
    
    
    '''
标签:

未经允许不得转载:作者:1617-卿同学, 转载或复制请以 超链接形式 并注明出处 拜师资源博客
原文地址:《2021-09-20:除自己的乘积,random pick with weight,二叉树的直径,加法器 add string》 发布于2021-09-21

分享到:
赞(0) 打赏

评论 抢沙发

评论前必须登录!

  注册



长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu3.3主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录