力扣
Shoko0116
这个作者很懒,什么都没留下…
展开
-
Leetcode 797. 所有可能的路径
797. 所有可能的路径class Solution: def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]: result = [] ans = [] def dfs(i): if i == len(graph) - 1: result.append(ans[:])原创 2022-02-28 22:42:12 · 78 阅读 · 0 评论 -
Leetcode 150. 逆波兰表达式求值
150. 逆波兰表达式求值class Solution: def evalRPN(self, tokens: List[str]) -> int: operation = ['+', '-', '*', '/'] nums = [] for num in tokens: if num not in operation: nums.append(float(num))原创 2022-02-18 22:08:35 · 99 阅读 · 0 评论 -
Leetcode 116. 填充每个节点的下一个右侧节点指针
116. 填充每个节点的下一个右侧节点指针Code"""# Definition for a Node.class Node: def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): self.val = val self.left = left self.right = right sel原创 2022-02-10 22:09:29 · 67 阅读 · 0 评论 -
Leetcode 103. 二叉树的锯齿形层序遍历
103. 二叉树的锯齿形层序遍历Ideas算法:DFS数据结构:二叉树Code时间复杂度:O(N),其中 N 为二叉树的节点数。每个节点会且仅会被遍历一次。空间复杂度:O(N)。我们需要维护存储节点的队列和存储节点值的双端队列,空间复杂度为 O(N)。# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# se原创 2022-02-09 19:11:44 · 179 阅读 · 0 评论 -
Leetcode 102. 二叉树的层序遍历
129. 求根节点到叶节点数字之和Ideas算法:递归数据结构:二叉树Code时间复杂度:O(N)其中 N 为二叉树的节点数,即遍历一棵二叉树的时间复杂度,每个结点只被访问一次。空间复杂度:O(Height)其中 Height 为二叉树的高度。# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self原创 2022-02-08 17:03:48 · 81 阅读 · 0 评论 -
Leetcode 22. 括号生成
22. 括号生成Ideasn很小,考虑深搜每次添加之前计算括号的数量,判断是否有效如果左括号数量不大于 n,我们可以放一个左括号。如果右括号数量小于左括号的数量,我们可以放一个右括号。Codeclass Solution {public: int nn; vector<string> ans; void dfs(int now, int zuo, int you, string& path) { if (now == 2 *原创 2022-01-23 21:15:57 · 208 阅读 · 0 评论 -
Leetcode 1267. 统计参与通信的服务器
1267. 统计参与通信的服务器Ideas用一行和一列去记录出现的个数,最后遍历全部元素统计一下C++class Solution {public: int countServers(vector<vector<int>>& grid) { int m = grid.size(); int n = grid[0].size(); vector<int> count_m(m), count_n(n);原创 2022-01-19 19:03:22 · 688 阅读 · 0 评论 -
Leetcode 面试题 16.10. 生存人数
面试题 16.10. 生存人数C++class Solution {public: int maxAliveYear(vector<int>& birth, vector<int>& death) { int ans = 0, length = birth.size(); vector<int> cnt(105,0); for(int i = 0; i < length;原创 2022-01-18 22:47:54 · 4932 阅读 · 0 评论 -
Leetcode 240. 搜索二维矩阵 II
240. 搜索二维矩阵 IIIdeas算法:二分查找Codeclass Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: m = len(matrix) n = len(matrix[0]) for row in matrix: if row[0] > target or row[n - 1] &l原创 2022-01-15 22:44:12 · 95 阅读 · 0 评论 -
Leetcode 33. 搜索旋转排序数组
33. 搜索旋转排序数组Ideas算法:二分查找Codeclass Solution: def search(self, nums: List[int], target: int) -> int: if target in nums: return nums.index(target) return -1class Solution {public: int search(vector<int>&原创 2022-01-14 00:33:00 · 170 阅读 · 0 评论 -
Leetcode 202. 快乐数
202. 快乐数Codeclass Solution: def isHappy(self, n: int) -> bool: def help(x): temp = list(map(int,[i for i in str(x)])) num = 0 for i in temp: num += i ** 2 return num a原创 2022-01-13 00:21:39 · 94 阅读 · 0 评论 -
Leetcode 118. 杨辉三角
118. 杨辉三角118. 杨辉三角Codeclass Solution: def generate(self, numRows: int) -> List[List[int]]: re = [] for i in range(numRows): values = [] for j in range(i + 1): if j == 0 or j == i:原创 2022-01-12 00:05:32 · 1556 阅读 · 0 评论 -
Leetcode 274. H 指数
274. H 指数Codeclass Solution: def hIndex(self, citations: List[int]) -> int: citations.sort(reverse=True) for i, v in enumerate(citations): if i + 1 > v: return i return len(citations)原创 2022-01-09 00:38:40 · 524 阅读 · 0 评论 -
Leetcode 268. 丢失的数字
268. 丢失的数字Codeclass Solution: def missingNumber(self, nums: List[int]) -> int: length = len(nums) nums.sort() for i in range(length): if nums[i] != i: return i return length原创 2022-01-08 23:57:51 · 53 阅读 · 0 评论 -
Leetcode 148. 排序链表
148. 排序链表Ideas算法:排序数据结构:链表直接读到列表里排序再写回原链表Code# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def sortList(self, head: Optional[原创 2022-01-07 23:23:55 · 280 阅读 · 0 评论 -
Leetcode 414. 第三大的数
414. 第三大的数Code时间复杂度:O(N)空间复杂度:O(1)class Solution: def thirdMax(self, nums: List[int]) -> int: l = [-2**31-1, -2**31-1, -2**31-1] for i in nums: if i > l[0]: l[2] = l[1] l[1] = l[0]原创 2022-01-06 21:25:39 · 434 阅读 · 0 评论 -
Leetcode 169. 多数元素
169. 多数元素Codeclass Solution: def majorityElement(self, nums: List[int]) -> int: counter = Counter(nums) return counter.most_common(1)[0][0]原创 2022-01-04 23:24:16 · 68 阅读 · 0 评论 -
Leetcode 507. 完美数
507. 完美数Ideas算法:迭代Code时间复杂度:O(N^(1/2))空间复杂度:O(1)class Solution: def checkPerfectNumber(self, num: int) -> bool: if num == 1: return False sum = 1 d = 2 while d * d <= num: if num % d原创 2021-12-31 15:48:59 · 296 阅读 · 0 评论 -
Leetcode 204. 计数质数
204. 计数质数Ideas算法:迭代埃氏筛Codeclass Solution: def countPrimes(self, n: int) -> int: l = [1 for _ in range(n)] for i in range(2,int(n ** 0.5) + 1): if l[i]: for j in range(i * i, n, i):原创 2021-12-31 14:25:59 · 375 阅读 · 0 评论 -
Leetcode 1925. 统计平方和三元组的数目
1925. 统计平方和三元组的数目Ideas算法:迭代Codeclass Solution: def countTriples(self, n: int) -> int: ans = 0 for a in range(1, n + 1): for b in range(a + 1, n + 1): c = sqrt(a ** 2 + b ** 2) if c % 1原创 2021-12-30 00:14:41 · 85 阅读 · 0 评论 -
Leetcode 1566. 重复至少 K 次且长度为 M 的模式
1566. 重复至少 K 次且长度为 M 的模式Ideas算法:迭代Code时间复杂度:O(N)空间复杂度:O(N)class Solution: def containsPattern(self, arr: List[int], m: int, k: int) -> bool: length = len(arr) # 如果长度不可能符合,那就提前结束 if(m * k > length): return F原创 2021-12-28 22:47:19 · 246 阅读 · 0 评论 -
Leetcode 1534. 统计好三元组
1534. 统计好三元组Ideas算法:迭代Code时间复杂度:O(n^3)空间复杂度:O(1)class Solution: def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int: l = len(arr) i = 0 j = 1 k = 2 re = 0 for k in range(2,原创 2021-12-27 10:57:45 · 382 阅读 · 0 评论 -
Leetcode 剑指 Offer II 056. 二叉搜索树中两个节点之和
剑指 Offer II 056. 二叉搜索树中两个节点之和Ideas算法:递归数据结构:二叉搜索树根据二叉搜索树的性质使用中序遍历将所有节点的值保存到一个数组遍历完成之后会得到一个有序的数组从而将问题转换成两数之和的问题Code# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = va原创 2021-12-20 17:41:29 · 354 阅读 · 0 评论 -
Leetcode 剑指 Offer 55 - II. 平衡二叉树
剑指 Offer 55 - II. 平衡二叉树Ideas算法:递归数据结构:二叉树Code# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def isBalanced(self, ro原创 2021-12-19 20:13:30 · 1327 阅读 · 0 评论 -
Leetcode 530. 二叉搜索树的最小绝对差
530. 二叉搜索树的最小绝对差Ideas算法:迭代 or 递归数据结构:二叉搜索树二叉搜索树中序遍历得到的值序列是递增有序的答案一定为升序数组相邻两个元素之差的最小值迭代或者递归来实现二叉树的中序遍历Code时间复杂度:O(n)其中 nn 为二叉搜索树节点的个数。每个节点在中序遍历中都会被访问一次且只会被访问一次空间复杂度:O(n)递归函数的空间复杂度取决于递归的栈深度,而栈深度在二叉搜索树为一条链的情况下会达到 O(n) 级别# Definition for a binary t原创 2021-12-18 20:40:39 · 368 阅读 · 0 评论 -
Leetcode 700. 二叉搜索树中的搜索
700. 二叉搜索树中的搜索Ideas算法:迭代数据结构:二叉搜索树Code时间复杂度:O(N)空间复杂度:O(1)# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = right原创 2021-12-18 20:33:34 · 199 阅读 · 0 评论 -
Leetcode 543. 二叉树的直径
530. 二叉搜索树的最小绝对差Ideas算法:迭代 or 递归数据结构:二叉搜索树二叉搜索树中序遍历得到的值序列是递增有序的任意两个元素之差的绝对值的最小值,答案一定为相邻两个元素之差的最小值迭代或者递归实现二叉搜索树的中序遍历Code法一:迭代实现时间复杂度:O(n):其中 n 为二叉搜索树节点的个数。每个节点在中序遍历中都会被访问一次且只会被访问一次空间复杂度:O(n):空间复杂度取决于栈深度,而栈深度在二叉树为一条链的情况下会达到 O(n) 的级别# Definition原创 2021-12-18 08:17:27 · 374 阅读 · 0 评论 -
Leetcode 剑指 Offer 25. 合并两个排序的链表
剑指 Offer 25. 合并两个排序的链表Ideas数据结构:链表Code时间复杂度:O(M+N),M,N 分别为链表 l1 , l2 的长度空间复杂度:O(1)# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def mergeTwoL原创 2021-12-15 22:16:53 · 410 阅读 · 1 评论 -
Leetcode 剑指 Offer 06. 从尾到头打印链表
剑指 Offer 06. 从尾到头打印链表Ideas数据结构:链表Code时间复杂度:O(n)空间复杂度:O(n),其中 n 指的是链表的元素个数# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def isPalindrome(self,原创 2021-12-14 21:48:11 · 373 阅读 · 0 评论 -
Leetcode 面试题 02.06. 回文链表
面试题 02.06. 回文链表Ideas数据结构:链表Code时间复杂度:O(n)空间复杂度:O(n),其中 n 指的是链表的元素个数# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def isPalindrome(self, head:原创 2021-12-13 18:57:47 · 200 阅读 · 0 评论 -
Leetcode 1290. 二进制链表转整数
1290. 二进制链表转整数Ideas数据结构:链表Code时间复杂度:O(N),其中 N 是给定链表的结点数目。空间复杂度:O(1)# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def getDecim原创 2021-12-12 15:19:29 · 177 阅读 · 0 评论 -
Leetcode 876. 链表的中间结点
876. 链表的中间结点Ideas数据结构:链表Code法一:双指针时间复杂度:O(N),其中 N 是给定链表的结点数目。空间复杂度:O(1)# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def mi原创 2021-12-11 14:20:27 · 620 阅读 · 0 评论 -
Leetcode 203. 移除链表元素
203. 移除链表元素Ideas数据结构:链表Code时间复杂度:O(N),N为链表长度空间复杂度:O(1)# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def removeElements(self,原创 2021-12-10 13:42:13 · 290 阅读 · 0 评论 -
Leetcode 160. 相交链表
1. 两数之和Ideas算法:迭代数据结构:哈希表Code法一:单次哈希时间复杂度:O(N)空间复杂度:O(N)class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: dic = {} for i in range(len(nums)): if target - nums[i] in dic:原创 2021-12-10 00:43:30 · 259 阅读 · 0 评论 -
Leetcode 19. 删除链表的倒数第 N 个结点
19. 删除链表的倒数第 N 个结点Ideas数据结构:链表Code法一:双指针时间复杂度:O(L),其中 L 是链表的长度空间复杂度:O(1)# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def r原创 2021-12-08 22:01:57 · 76 阅读 · 0 评论 -
Leetcode 83. 删除排序链表中的重复元素
83. 删除排序链表中的重复元素Ideas数据结构:链表Code时间复杂度:O(N)空间复杂度:O(1)# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def deleteDuplicates(self,原创 2021-12-07 17:22:37 · 70 阅读 · 0 评论 -
Leetcode 206. 反转链表
206. 反转链表Ideas数据结构:链表图解链接Code时间复杂度:O(N),n是链表的长度空间复杂度:O(1)# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def reverseList(self,原创 2021-12-07 10:51:03 · 174 阅读 · 0 评论 -
Leetcode 9. 回文数
1. 两数之和Ideas算法:迭代数据结构:哈希表Code法一:单次哈希时间复杂度:O(N)空间复杂度:O(N)class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: dic = {} for i in range(len(nums)): if target - nums[i] in dic:原创 2021-12-04 22:38:37 · 656 阅读 · 0 评论 -
LeetCode 7. 整数反转
7. 整数反转Ideas算法:迭代数据结构:哈希表Code法二:转化为正数,判断结果是否超出范围时间复杂度:O(N)空间复杂度:O(N)class Solution: def reverse(self, x: int) -> int: res = 0 flag = 0 if(x < 0): x = -x flag = 1 length = len(str(x原创 2021-12-04 01:42:03 · 2355 阅读 · 1 评论 -
LeetCode 1. 两数之和
1. 两数之和Ideas算法:迭代数据结构:哈希表Code法一:单次哈希时间复杂度:O(N)空间复杂度:O(N)class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: dic = {} for i in range(len(nums)): if target - nums[i] in dic:原创 2021-12-02 22:35:07 · 430 阅读 · 0 评论