import threading import time 简单的创建 def run(n): print("task", n) time.sleep(1) print('2s') time.sleep(1) print('1s') time.sleep(1) print('0s') time.sleep(1) if __name__ == '__main__': t1 = threading.Thread(target=run, args=("t1",)) t2 = threading.Thread(target=run, args=("t2",)) t1.start() t2.start() 通过类创建 class MyThread(threading.Thread): def __init__(self, n): super(MyThread, self).__init__() # 重构run函数必须要写 self.n = n def run(self): print("task", self.n) time.sleep(1) print('2s') time.sleep(1) print('1s') time.sleep(1)
丑数系列 1.丑数 题目: https://leetcode-cn.com/problems/ugly-number/ 思路: 就是让这个数字不断地除以2.3.5 如果最后变成了1 就说明是个丑数 代码: class Solution: def isUgly(self, num: int) -> bool: if num<=-231 or num>=231-1: return False while num >1: if num %2
Tf-Idf 参考:https://www.jb51.net/article/142132.htm 代码:(使用gensim) import jieba from gensim import corpora, models, similarities doc0 = &quo
梯度提升决策树(GBDT) GBDT(Gradient Boosting Decision Tree)是一种迭代的决策树算法,由多棵决策树组成,所有树的结论累加起来作为最终答案。 回归树 选择最优切分变量
两两交换链表中的节点 题目: https://leetcode-cn.com/problems/swap-nodes-in-pairs/ 思路: 先把第二位储存起来,然后将后面的递归操作后,再把第二位指向第一位,完成换位 代码: # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None):
种花问题(新年快乐!2021第一题) 新年快乐!2021年第一题,每日一题!希望2021年LC和github可以全绿!加油! https://leetcode-cn.com/problems/can-place-flowers/ 代码如下: class Solution: def
零钱兑换 https://leetcode-cn.com/problems/coin-change/ 以我目前的水平做出来有点吃力,看了思路才做出来 class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp = [float('inf')] * (amount + 1) dp[0] = 0 for i in range(amount+1): for coin in coins: # if i >= coin: dp[i] = min(dp[i],dp[i-coin]+1) return -1 if (dp[-1] == float("inf"))
去除重复字母 一开始看到题目感觉挺简单的,没想到对现在的我挺有难度。。 https://leetcode-cn.com/problems/remove-duplicate-letters/ #1 class Solution: def removeDuplicateLetters(s: str): res = "" while s: #用递归也可以 loc = min(map(s.rindex,s)) #s.rindex是返回列表