- 博客(63)
- 收藏
- 关注
原创 LeetCode题解(0963):最小面积矩形II(Python)
题目:原题链接(中等)标签:几何、数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N2logN)O(N^2logN)O(N2logN)O(N2)O(N^2)O(N2)92ms (92.68%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def minAreaFreeRect(self, points: List[List[int]]) -> float:
2020-12-08 22:44:37
32
原创 LeetCode题解(0587):安装栅栏(Python)
题目:原题链接(困难)标签:几何、数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(NlogN)O(NlogN)O(NlogN)O(N)O(N)O(N)108ms (87.27%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def orientation(self, p, q, r): return (q[1] - p[1]) * (r[0] - q
2020-12-08 22:44:33
31
原创 LeetCode题解(1668):最大重复子字符串(Python)
题目:原题链接(简单)标签:字符串、双指针解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N1×N2)O(N1×N2)O(N1×N2)O(1)O(1)O(1)36ms (76.94%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def maxRepeating(self, sequence: str, word: str) -> int: s1,
2020-12-08 22:44:29
53
原创 LeetCode题解(1652):拆炸弹(Python)
题目:原题链接(简单)标签:数组解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)52ms (44.78%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def decrypt(self, code: List[int], k: int) -> List[int]: if k == 0:
2020-12-08 22:44:26
53
原创 LeetCode题解(1528):重新排列字符串(Python)
题目:原题链接(简单)标签:数组、排序解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)48ms (46.84%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def restoreString(self, s: str, indices: List[int]) -> str: res = [""] * l
2020-12-08 22:44:22
33
原创 LeetCode题解(1523):在区间范围内统计奇数数目(Python)
题目:原题链接(简单)标签:数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(1)O(1)O(1)O(1)O(1)O(1)44ms (33.75%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def countOdds(self, low: int, high: int) -> int: if (high - low) % 2 == 1:
2020-12-08 22:44:18
23
原创 LeetCode题解(0228):汇总区间(Python)
题目:原题链接(简单)标签:数组解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(1)O(1)O(1)36ms (77.80%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def summaryRanges(self, nums: List[int]) -> List[str]: if not nums:
2020-12-08 22:44:14
18
原创 LeetCode题解(LCP22):黑白方格画(Python)
题目:原题链接(简单)标签:数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N2)O(N^2)O(N2)O(N)O(N)O(N)32ms (93.87%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def paintingPlan(self, n: int, k: int) -> int: if k == n * n:
2020-12-08 22:44:09
31
原创 LeetCode题解(LCP17):速算机器人(Python)
题目:原题链接(简单)标签:数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(S)O(S)O(S)O(1)O(1)O(1)32ms (94.86%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def calculate(self, s: str) -> int: x, y = 1, 0 for ch in s:
2020-12-08 22:44:03
23
原创 LeetCode题解(LCP11):期望个数统计(Python)
题目:原题链接(简单)标签:数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(1)O(1)O(1)76ms (76.95%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def expectNumber(self, scores: List[int]) -> int: return len(set(scores)).
2020-12-08 22:43:58
21
原创 LeetCode题解(LCP26):导航装置(Python)
题目:原题链接(困难)标签:树、二叉树、动态规划解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)896ms (94.74%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: # 对于每个三叉节点,至少有两边需要放导航 def __init__(self): self.ans = 0 def na
2020-12-07 22:05:51
21
原创 LeetCode题解(LCP07):传递信息(Python)
题目:原题链接(简单)标签:图、深度优先搜索解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(NK)O(N^K)O(NK)O(NK)O(N^K)O(NK)52ms (39.84%)Ans 2 (Python)Ans 3 (Python)解法一:def build_graph(edges): graph = collections.defaultdict(set) for edge in edges:
2020-12-07 22:05:36
27
原创 LeetCode题解(LCP06):拿硬币(Python)
题目:原题链接(简单)标签:数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(1)O(1)O(1)44ms (35.31%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def minCount(self, coins: List[int]) -> int: return sum((coin + 1) // 2 for c
2020-12-07 22:05:32
49
原创 LeetCode题解(LCP03):机器人大冒险(Python)
题目:原题链接(中等)标签:几何、数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N+M)O(N+M)O(N+M) : 其中N为命令长度,M为障碍数量O(M)O(M)O(M)28ms (99.58%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def robot(self, command: str, obstacles: List[List[int]], x:
2020-12-07 22:05:29
20
原创 LeetCode题解(LCP02):分式化简(Python)
题目:原题链接(简单)标签:数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(1)O(1)O(1)36ms (82.12%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def fraction(self, cont: List[int]) -> List[int]: n1, n2 = cont.pop(), 1
2020-12-07 22:05:26
29
原创 LeetCode题解(LCP01):猜数字(Python)
题目:原题链接(简单)标签:数组解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(1)O(1)O(1)28ms (98.29%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def game(self, guess: List[int], answer: List[int]) -> int: ans = 0 f
2020-12-07 22:05:18
25
原创 LeetCode题解(0528):按权重随机选择(Python)
题目:原题链接(中等)标签:随机、二分查找解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(logW)O(logW)O(logW)O(W)O(W)O(W)312ms (43.72%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def __init__(self, w: List[int]): self.nums = w self.pr
2020-12-07 22:05:15
38
1
原创 LeetCode题解(0519):随机翻转矩阵(Python)
题目:原题链接(中等)标签:随机、哈希表解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(1)O(1)O(1)O(F)O(F)O(F)68ms (74.07%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def __init__(self, n_rows: int, n_cols: int): self.n_rows, self.n_cols = n_
2020-12-07 22:05:11
30
原创 LeetCode题解(0497):非重叠矩形中的随机点(Python)
题目:原题链接(中等)标签:随机、二分查找解法时间复杂度空间复杂度执行用时Ans 1 (Python)每次抽取 = O(N)O(N)O(N)O(N)O(N)O(N)164ms (98.48%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: # 非重叠、轴对齐矩形 def __init__(self, rects: List[List[int]]): self.r
2020-12-07 22:05:07
24
原创 LeetCode题解(1382):将二叉搜索树变平衡(Python)
题目:原题链接(中等)标签:树、二叉树、二叉搜索树解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)240ms (79.27%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def inorder(self, node, lst): if node: self.inorder(node.l
2020-12-07 22:05:03
30
原创 LeetCode题解(1373):二叉搜索子树的最大键值和(Python)
题目:原题链接(困难)标签:树、二叉树、二叉搜索树解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)476ms (42.39%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def __init__(self): self.ans = 0 def maxSumBST(self, root: TreeNo
2020-12-06 21:37:33
18
原创 LeetCode题解(1038):把二叉搜索树转换为累加树(Python)
题目:原题链接(中等)标签:树、二叉树、二叉搜索树解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)40ms (71.31%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def __init__(self): self.total = 0 def bstToGst(self, root: TreeNo
2020-12-06 21:37:30
19
原创 LeetCode题解(1227):飞机座位分配概率(Python)
题目:原题链接(中等)标签:脑筋急转弯解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(1)O(1)O(1)O(1)O(1)O(1)32ms (95.27%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def nthPersonGetsNthSeat(self, n: int) -> float: return 0.5 if n > 1 el
2020-12-06 21:37:26
44
原创 LeetCode题解(0777):在LR字符串中交换相邻字符(Python)
题目:原题链接(中等)标签:脑筋急转弯、双指针解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N1+N2)O(N1+N2)O(N1+N2)O(N1+N2)O(N1+N2)O(N1+N2)56ms (65.87%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def canTransform(self, start: str, end: str) -> bool:
2020-12-06 21:37:23
36
原创 LeetCode题解(0319):灯泡开关(Python)
题目:原题链接(中等)标签:脑筋急转弯、数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(1)O(1)O(1)O(1)O(1)O(1)44ms (31.41%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def bulbSwitch(self, n: int) -> int: return int(pow(n, 0.5)) if n >
2020-12-06 21:37:19
15
原创 LeetCode题解(LCP05):团队成员发硬币(Python)
题目:原题链接(困难)标签:线段树解法时间复杂度空间复杂度执行用时Ans 1 (Python)每次操作 = O(N)O(N)O(N) (在每个节点只有一个子节点时出现)O(N)O(N)O(N)超出时间限制Ans 2 (Python)每次操作 = O(logN)O(logN)O(logN)O(N)O(N)O(N)3464ms (49.06%)Ans 3 (Python)解法一(将每个成员作为节点的包含lazy属性的树结构):class Solu
2020-12-06 21:37:15
18
原创 LeetCode题解(1306):跳跃游戏III(Python)
题目:原题链接(中等)标签:广度优先搜索、深度优先搜索、递归、记忆化递归解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)44ms (88.76%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def canReach(self, arr: List[int], start: int) -> bool: s
2020-12-06 21:37:11
34
原创 LeetCode题解(0395):至少有K个重复字符的最长子串(Python)
题目:原题链接(中等)标签:递归、记忆化递归、分治算法、滑动窗口解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(2N)O(2^N)O(2N)O(2N)O(2^N)O(2N)120ms (7.32%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: @functools.lru_cache(None) def longestSubstring(self, s: str
2020-12-06 21:37:07
22
原创 LeetCode题解(LCP04):棋盘放2×1的纸牌的最大覆盖面积(Python)
题目:原题链接(困难)标签:图、广度优先搜索、贪心算法解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N2)O(N^2)O(N2)O(N)O(N)O(N)56ms (22.67%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def domino(self, n: int, m: int, broken: List[List[int]]) -> int:
2020-12-06 21:37:02
60
原创 LeetCode题解(1670):设计前中后队列(Python)
题目:原题链接(中等)标签:设计、链表、队列解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N) : 其中N为操作数O(N)O(N)O(N)96ms (26.96%)Ans 2 (Python)Ans 3 (Python)解法一(双端队列):class FrontMiddleBackQueue: class _Node: __slots__ = ("val", "prev", "next
2020-12-06 21:36:58
11
原创 LeetCode题解(1669):合并链表(Python)
题目:原题链接(中等)标签:链表解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N1+N2)O(N1+N2)O(N1+N2)O(1)O(1)O(1)456ms (29.05%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def mergeInBetween(self, list1: ListNode, a: int, b: int, list2: ListNode)
2020-12-05 22:36:15
22
原创 LeetCode题解(0398):随机数索引(Python)
题目:原题链接(中等)标签:随机、蓄水池抽样解法时间复杂度空间复杂度执行用时Ans 1 (Python)构造 = O(1)O(1)O(1) ; 随机 = O(N)O(N)O(N)O(1)O(1)O(1)320ms (96.61%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def __init__(self, nums: List[int]): self.nums
2020-12-05 22:36:04
16
原创 LeetCode题解(0382):链表随机节点(Python)
题目:原题链接(中等)标签:随机、蓄水池抽样解法时间复杂度空间复杂度执行用时Ans 1 (Python)构造 = O(1)O(1)O(1) ; 随机= O(N)O(N)O(N)O(1)O(1)O(1)216ms (47.03%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def __init__(self, head: ListNode): self.head =
2020-12-05 22:35:59
14
原创 LeetCode题解(0478):在圆内随机生成点(Python)
题目:原题链接(中等)标签:随机、拒绝采样、数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)期望 : O(1)O(1)O(1)O(1)O(1)O(1)168ms (65.61%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def __init__(self, radius: float, x_center: float, y_center: float):
2020-12-05 22:35:51
21
原创 LeetCode题解(0470):用Rand7()实现Rand10()(Python)
题目:原题链接(中等)标签:随机、拒绝采样解法时间复杂度空间复杂度执行用时Ans 1 (Python)期望 = O(1)O(1)O(1)O(1)O(1)O(1)316ms (89.16%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def rand10(self): idx = 50 while idx >= 40: i1
2020-12-05 22:35:47
10
原创 LeetCode题解(0329):矩阵中的最长递增路径(Python)
题目:原题链接(困难)标签:深度优先搜索、记忆化递归、拓扑排序解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N×M)O(N×M)O(N×M)O(N×M)O(N×M)O(N×M)552ms (42.32%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def __init__(self): self.s1, self.s2 = 0, 0
2020-12-05 22:35:43
16
原创 LeetCode题解(0721):账户合并(Python)
题目:原题链接(中等)标签:并查集、深度优先搜索解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)272ms (61.28%)Ans 2 (Python)Ans 3 (Python)解法一:class DSU: def __init__(self, n): self.array = [i for i in range(n)] self.size
2020-12-05 22:35:39
29
原创 LeetCode题解(0547):朋友圈(Python)
题目:原题链接(中等)标签:并查集、深度优先搜索解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N2)O(N^2)O(N2)O(N)O(N)O(N)64ms (52.11%)Ans 2 (Python)Ans 3 (Python)解法一:class DSU: def __init__(self, n): self.array = [i for i in range(n)] self.si
2020-12-05 22:35:35
39
原创 LeetCode题解(0399):除法求值(Python)
题目:原题链接(中等)标签:图、并查集、深度优先搜索解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(Q×N)O(Q×N)O(Q×N) : 其中N为变量数O(N)O(N)O(N)40ms (62.76%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def calcEquation(self, equations: List[List[str]], values: Lis
2020-12-05 22:35:30
18
原创 LeetCode题解(0200):岛屿数量(Python)
题目:原题链接(中等)标签:深度优先搜索、广度优先搜索、并查集解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N×M)O(N×M)O(N×M)O(N×M)O(N×M)O(N×M)152ms (5.21%)Ans 2 (Python)Ans 3 (Python)解法一(并查集):class DSU: def __init__(self, n): self.array = [i for i in range
2020-12-05 22:35:26
19
1
空空如也
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人 TA的粉丝