其他分享
首页 > 其他分享> > 每日一练 — 2022.01.27

每日一练 — 2022.01.27

作者:互联网

文章目录


一,路径总和

1,程序简介

示例 1:

在这里插入图片描述

示例 2:

在这里插入图片描述

示例 3:

提示:

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Fri Jan 28 19:37:50 2022
Function: 路径总和
@author: 小梁aixj
"""
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution:
    def hasPathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: bool
        """
        if root is None:
            return False
        if sum == root.val and root.left is None and root.right is None:
            return True
        return self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val)

二,单词拆分

1,程序简介

说明:

  1. 拆分时可以重复使用字典中的单词。
  2. 你可以假设字典中没有重复的单词。

示例 1:

示例 2:

示例 3:

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Fri Jan 28 19:42:32 2022
Function: 单词拆分
@author: 小梁aixj
"""
class Solution(object):
    def wordBreak(self, s, wordDict):
        """
        :type s: str
        :type wordDict: List[str]
        :rtype: bool
        """
        if not s:
            return True
        lenth = len(s)
        dp = [False for i in range(lenth + 1)]
        dp[0] = True
        for i in range(1, lenth + 1):
            for j in range(i):
                if dp[j] and s[j:i] in wordDict:
                    dp[i] = True
                    break
        return dp[-1]

三,O(1) 时间插入、删除和获取随机元素

1,程序简介

注意:

示例:

// 初始化一个空的集合。
RandomizedCollection collection = new RandomizedCollection();

// 向集合中插入 1 。返回 true 表示集合不包含 1 。
collection.insert(1);

// 向集合中插入另一个 1 。返回 false 表示集合包含 1 。集合现在包含 [1,1] 。
collection.insert(1);

// 向集合中插入 2 ,返回 true 。集合现在包含 [1,1,2] 。
collection.insert(2);

// getRandom 应当有 2/3 的概率返回 1 ,1/3 的概率返回 2 。
collection.getRandom();

// 从集合中删除 1 ,返回 true 。集合现在包含 [1,2] 。
collection.remove(1);

// getRandom 应有相同概率返回 1 和 2 。
collection.getRandom();

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Fri Jan 28 19:44:35 2022
Function: O(1) 时间插入、删除和获取随机元素
@author: 小梁aixj
"""
class RandomizedSet:
    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.dict = {}
        self.list = []
    def insert(self, val: int) -> bool:
        """
        Inserts a value to the set. Returns true if the set did not already contain the specified element.
        """
        if val in self.dict:
            return False
        self.dict[val] = len(self.list)
        self.list.append(val)
        return True
    def remove(self, val: int) -> bool:
        """
        Removes a value from the set. Returns true if the set contained the specified element.
        """
        if val in self.dict:
            last_element, idx = self.list[-1], self.dict[val]
            self.list[idx], self.dict[last_element] = last_element, idx
            self.list.pop()
            del self.dict[val]
            return True
        return False
    def getRandom(self) -> int:
        """
        Get a random element from the set.
        """
        return choice(self.list)

标签:27,return,val,示例,true,每日,root,self,2022.01
来源: https://blog.csdn.net/m0_62617719/article/details/122735665