随笔分类 - LeetCode/算法题
摘要:最长公共子串 和 最长公共子序列 子串必须连续, 子序列不要求连续,次序不变即可 最长公共子串 dp[i][j] 表示以A[i]为结尾的A 的子串 和 以B[j]为结尾的B 的子串的 最长公共子串的长度 初始化: if A[0] == B[0]: dp[0][0] = 1 else: dp[0][0
阅读全文
摘要:LC785.判断二分图 LeetCode 785 方法一: BFS + 染色 class Solution: def isBipartite(self, graph: List[List[int]]) -> bool: # BFS from collections import deque n =
阅读全文
摘要:# 如无特殊要求,可以直接使用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 # 位运算,也就是
阅读全文
摘要: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
阅读全文

浙公网安备 33010602011771号