随笔分类 -  LeetCode/算法题

摘要:最长公共子串 和 最长公共子序列 子串必须连续, 子序列不要求连续,次序不变即可 最长公共子串 dp[i][j] 表示以A[i]为结尾的A 的子串 和 以B[j]为结尾的B 的子串的 最长公共子串的长度 初始化: if A[0] == B[0]: dp[0][0] = 1 else: dp[0][0 阅读全文
posted @ 2021-09-15 17:43 pas_a_pas 阅读(65) 评论(0) 推荐(0)
摘要:LC785.判断二分图 LeetCode 785 方法一: BFS + 染色 class Solution: def isBipartite(self, graph: List[List[int]]) -> bool: # BFS from collections import deque n = 阅读全文
posted @ 2021-09-15 11:55 pas_a_pas 阅读(137) 评论(0) 推荐(0)
摘要:# 如无特殊要求,可以直接使用pow(x, y, mod) 函数 def fast_power(x, y, z): res = 1 while y: if y&1 == 1: # y&1 是取y的二进制最后一位, 用来判断是否为奇数 res = res*x%z y = y>> 1 # 位运算,也就是 阅读全文
posted @ 2021-09-06 22:39 pas_a_pas 阅读(162) 评论(0) 推荐(0)
摘要:class UnionFind: def __init__(self, n): self.root = [i for i in range(n)] self.rank = [1]*n self.cnt = n # 用来统计孤岛个数 def find(self, x): if x == self.ro 阅读全文
posted @ 2021-09-04 22:22 pas_a_pas 阅读(58) 评论(0) 推荐(0)