- 博客(1370)
- 收藏
- 关注
原创 LeetCode题解(0753):破解保险箱(Python)
题目:原题链接(困难)标签:深度优先搜索、数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N×KN)O(N×K^N)O(N×KN)O(N×KN)O(N×K^N)O(N×KN)84ms (9.38%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def crackSafe(self, n: int, k: int) -> str: visited
2021-01-21 22:54:09
7
原创 LeetCode题解(0332):重新安排行程(Python)
题目:原题链接(中等)标签:图、深度优先搜索解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(NlogN)O(NlogN)O(NlogN)O(N)O(N)O(N)44ms (87.47%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def findItinerary(self, tickets: List[List[str]]) -> List[str]:
2021-01-21 22:54:04
3
原创 LeetCode题解(0324):摆动排序II(Python)
题目:原题链接(中等)标签:排序、数组解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)1992ms (19.20%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def wiggleSort(self, nums: List[int]) -> None: size = len(nums)
2021-01-21 22:54:00
5
原创 LeetCode题解(0322):零钱兑换(Python)
题目:原题链接(中等)标签:回溯算法、动态规划、记忆化递归解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(C×A)O(C×A)O(C×A)O(A)O(A)O(A)1116ms (87.81%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def coinChange(self, coins: List[int], amount: int) -> int:
2021-01-21 22:53:56
3
原创 LeetCode题解(0318):最大单词长度乘积(Python)
题目:原题链接(中等)标签:位运算、堆解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(NlogN)O(NlogN)O(NlogN)O(N)O(N)O(N)6072ms (8.98%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def maxProduct(self, words: List[str]) -> int: if not words:
2021-01-21 22:53:51
2
原创 LeetCode题解(0310):最小高度树(Python)
题目:原题链接(中等)标签:图、广度优先搜索解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)88ms (72.97%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
2021-01-21 22:53:44
2
原创 LeetCode题解(0309):最佳买卖股票时机含冷冻期(Python)
题目:原题链接(中等)标签:贪心算法、动态规划解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)44ms (58.76%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def maxProfit(self, prices: List[int]) -> int: if len(prices) <= 1:
2021-01-21 22:53:40
4
原创 LeetCode题解(0306):累加数(Python)
题目:原题链接(中等)标签:回溯算法、字符串解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N3)O(N^3)O(N3)O(N)O(N)O(N)36ms (86.49%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def isAdditiveNumber(self, num: str) -> bool: for i in range(1, len
2021-01-21 22:53:35
2
原创 LeetCode题解(0304):二维区域和检索-矩阵不可变(Python)
题目:原题链接(中等)标签:数组、动态规划解法时间复杂度空间复杂度执行用时Ans 1 (Python)构造 = O(N×M)O(N×M)O(N×M) ; 查询 = O(1)O(1)O(1)O(N×M)O(N×M)O(N×M)68ms (94.43%)Ans 2 (Python)Ans 3 (Python)解法一:class NumMatrix: def __init__(self, matrix: List[List[int]]):
2021-01-21 22:53:30
3
原创 LeetCode题解(1202):交换字符串中的元素(Python)
题目:原题链接(中等)标签:并查集、数组、字符串解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(NlogN)O(NlogN)O(NlogN)O(N)O(N)O(N)768ms (59.09%)Ans 2 (Python)Ans 3 (Python)解法一:class DSU: def __init__(self, n: int): self.array = [i for i in range(n)]
2021-01-21 22:53:25
3
原创 LeetCode题解(0947):移除最多的同行或同列石头(Python)
题目:原题链接(中等)标签:并查集、深度优先搜索解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)72ms (96.80%)Ans 2 (Python)Ans 3 (Python)解法一:class DSU: def __init__(self, n: int): self.array = [i for i in range(n)] self.s
2021-01-20 21:52:50
8
原创 LeetCode题解(0432):全O(1)的数据结构(Python)
题目:原题链接(困难)标签:设计、链表解法时间复杂度空间复杂度执行用时Ans 1 (Python)dec = O(N)O(N)O(N) ; 其他操作 = O(1)O(1)O(1)O(N)O(N)O(N)5364ms (5.71%)Ans 2 (Python)所有操作 = O(1)O(1)O(1)O(N)O(N)O(N)80ms (75.24%)Ans 3 (Python)解法一:class AllOne: def __init__(s
2021-01-20 21:52:45
7
原创 LeetCode题解(0289):生命游戏(Python)
题目:原题链接(中等)标签:数组解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N×M)O(N×M)O(N×M)O(1)O(1)O(1)44ms (42.99%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def gameOfLife(self, board: List[List[int]]) -> None: m, n = len(board)
2021-01-20 21:52:41
7
原创 LeetCode题解(0287):寻找重复数(Python)
题目:原题链接(中等)标签:数组、双指针、二分查找解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)44ms (76.83%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def findDuplicate(self, nums: List[int]) -> int: return collections.
2021-01-20 21:52:37
原创 LeetCode题解(0284):顶端迭代器(Python)
题目:原题链接(中等)标签:设计解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(1)O(1)O(1)O(N)O(N)O(N)44ms (44.22%)Ans 2 (Python)Ans 3 (Python)解法一:class PeekingIterator: def __init__(self, iterator): self.iterator = iterator self.top =
2021-01-20 21:52:32
4
原创 LeetCode题解(0282):给表达式添加运算符(Python)
题目:原题链接(困难)标签:分治算法、回溯算法解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(4N)O(4^N)O(4N)O(N)O(N)O(N)7500ms (13.93%)Ans 2 (Python)Ans 3 (Python)解法一(极致暴力):class Solution: def __init__(self): self.size = 0 self.num = []
2021-01-20 21:52:28
8
原创 LeetCode题解(0279):完全平方数(Python)
题目:原题链接(中等)标签:数学、深度优先搜索、动态规划解法时间复杂度空间复杂度执行用时Ans 1 (Python)––超出时间限制Ans 2 (Python)O(N)O(\sqrt{N})O(N)O(1)O(1)O(1)40ms (99.00%)Ans 3 (Python)解法一(暴力解法):class Solution: def __init__(self): self.squares = [] def
2021-01-20 21:52:24
原创 LeetCode题解(0275):H指数II(Python)
题目:原题链接(中等)标签:二分查找、数组解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(logN)O(logN)O(logN)O(1)O(1)O(1)40ms (85.20%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def hIndex(self, citations: List[int]) -> int: if not citations
2021-01-20 21:52:17
4
原创 LeetCode题解(0260):只出现一次的数字III(Python)
题目:原题链接(中等)标签:位运算解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(1)O(1)O(1)40ms (87.28%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def singleNumber(self, nums: List[int]) -> List[int]: # 异或找到出现次数为奇数次的二进制位
2021-01-20 21:52:12
原创 LeetCode题解(0241):为运算表达式设计优先级(Python)
题目:原题链接(中等)标签:分治算法、递归解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(2N)O(2^N)O(2N)O(2N)O(2^N)O(2N)44ms (59.21%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def diffWaysToCompute(self, input: str) -> List[int]: # 处理只剩下数字的
2021-01-20 21:52:07
8
原创 LeetCode题解(0238):除自身以外数组的乘积(Python)
题目:原题链接(中等)标签:数组解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(1)O(1)O(1)56ms (99.62%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: if len(nums) == 1:
2021-01-19 21:33:53
6
原创 LeetCode题解(0229):求众数II(Python)
题目:原题链接(中等)标签:数组、数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(1)O(1)O(1)48ms (70.08%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def majorityElement(self, nums: List[int]) -> List[int]: # 摩尔投票 n1
2021-01-19 21:33:49
8
原创 LeetCode题解(1723):完成所有工作的最短时间(Python)
题目:原题链接(困难)标签:回溯算法、递归解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(2N)O(2^N)O(2N)O(N)O(N)O(N)72ms (79.91%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def __init__(self): self.jobs = [] self.ans = 0 self.k
2021-01-19 21:33:45
17
原创 LeetCode题解(1722):执行交换操作后的最小汉明距离(Python)
题目:原题链接(中等)标签:贪心算法、深度优先搜索、并查集解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)792ms (23.27%)Ans 2 (Python)Ans 3 (Python)解法一:class DSU: def __init__(self, n: int): self.array = [i for i in range(n)]
2021-01-19 21:33:37
6
原创 LeetCode题解(1721):交换链表中的节点(Python)
题目:原题链接(中等)标签:链表解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(1)O(1)O(1)876ms (39.10%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def swapNodes(self, head: ListNode, k: int) -> ListNode: n1 = n2 = head
2021-01-19 21:33:32
3
原创 LeetCode题解(1720):解码异或后的数组(Python)
题目:原题链接(简单)标签:位运算解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(1)O(1)O(1)56ms (67.80%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def decode(self, encoded: List[int], first: int) -> List[int]: ans = [first]
2021-01-19 21:33:28
8
原创 LeetCode题解(1719):重构一棵树的方案数(Python)
题目:原题链接(困难)标签:分治算法、图、深度优先搜索、树、递归解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N2)O(N^2)O(N2)O(N)O(N)O(N)2456ms (31.82%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def __init__(self): self.graph = collections.defaultdict(s
2021-01-19 21:33:24
6
原创 LeetCode题解(1718):构建字典序最大的可行序列(Python)
题目:原题链接(中等)标签:回溯算法、递归解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(2N)O(2^N)O(2N)O(N)O(N)O(N)40ms (97.55%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def __init__(self): self.size, self.dp = 0, [] self.ans = []
2021-01-19 21:33:17
13
原创 LeetCode题解(1717):删除子字符串的最大得分(Python)
题目:原题链接(中等)标签:字符串、贪心算法解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)1056ms (25.82%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def maximumGain(self, s: str, x: int, y: int) -> int: lst = [""]
2021-01-19 21:33:13
12
原创 LeetCode题解(1716):计算力扣银行的钱(Python)
题目:原题链接(简单)标签:数学、贪心算法解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(logN)O(logN)O(logN)O(1)O(1)O(1)36ms (84.72%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def totalMoney(self, n: int) -> int: week, surplus = divmod(n,
2021-01-19 21:33:08
10
原创 LeetCode题解(0223):矩形面积(Python)
题目:原题链接(中等)标签:几何、数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(1)O(1)O(1)O(1)O(1)O(1)72ms (14.66%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: @staticmethod def count(a, b, c, d): if a <= c <= b <= d:
2021-01-18 21:38:36
7
原创 LeetCode题解(0221):最大正方形(Python)
题目:原题链接(中等)标签:动态规划解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N×M)O(N×M)O(N×M)O(N×M)O(N×M)O(N×M)92ms (53.47%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def maximalSquare(self, matrix: List[List[str]]) -> int: m, n =
2021-01-18 21:38:32
8
原创 LeetCode题解(0220):存在重复元素III(Python)
题目:原题链接(中等)标签:排序、有序映射解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N×K)O(N×K)O(N×K)O(1)O(1)O(1)超出时间限制Ans 2 (Python)O(N)O(N)O(N)O(K)O(K)O(K)52ms (85.77%)Ans 3 (Python)解法一:class Solution: def containsNearbyAlmostDuplicate(self, nums: L
2021-01-18 21:38:28
16
原创 LeetCode题解(0213):打家劫舍II(Python)
题目:原题链接(中等)标签:动态规划解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)40ms (57.73%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def rob(self, nums: List[int]) -> int: if len(nums) == 1: retur
2021-01-18 21:38:23
13
原创 LeetCode题解(0212):单词搜索II(Python)
题目:原题链接(困难)标签:回溯算法、字典树、深度优先搜索解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(M×N×26L)O(M×N×26^L)O(M×N×26L)O(M×N)O(M×N)O(M×N)9848ms (5.06%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def findWords(self, board: List[List[str]], words
2021-01-18 21:38:19
9
原创 LeetCode题解(0211):添加与搜索单词-数据结构设计(Python)
题目:原题链接(中等)标签:设计、字典树、回溯算法解法时间复杂度空间复杂度执行用时Ans 1 (Python)addWord = O(L)O(L)O(L) ; search = O(2L)O(2^L)O(2L)O(W×L)O(W×L)O(W×L)396ms (18.46%)Ans 2 (Python)Ans 3 (Python)解法一:class WordDictionary: def __init__(self):
2021-01-18 21:38:14
6
原创 LeetCode题解(0209):长度最小的子数组(Python)
题目:原题链接(中等)标签:数组、滑动窗口、双指针、二分查找解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(1)O(1)O(1)52ms (66.10%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def minSubArrayLen(self, s: int, nums: List[int]) -> int: ans =
2021-01-18 21:38:10
6
原创 LeetCode题解(0208):实现Trie(前缀树)(Python)
题目:原题链接(中等)标签:设计、字典树解法时间复杂度空间复杂度执行用时Ans 1 (Python)所有操作 = O(L)O(L)O(L)O(W+L)O(W+L)O(W+L)120ms (92.58%)Ans 2 (Python)Ans 3 (Python)解法一:class Trie: def __init__(self): self.root = {} def insert(self, word: str
2021-01-18 21:38:06
6
原创 LeetCode题解(0201):数字范围按位与(Python)
题目:原题链接(中等)标签:位运算、数学解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(logN)O(logN)O(logN)O(1)O(1)O(1)76ms (32.26%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def rangeBitwiseAnd(self, m: int, n: int) -> int: ans = 0
2021-01-18 21:38:02
6
原创 LeetCode题解(0179):最大数(Python)
题目:原题链接(中等)标签:排序解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(NlogN)O(NlogN)O(NlogN)O(logN)O(logN)O(logN)44ms (69.94%)Ans 2 (Python)Ans 3 (Python)解法一:class Solution: def largestNumber(self, nums: List[int]) -> str: ans = "
2021-01-18 21:37:53
6
空空如也
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人 TA的粉丝