自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(333)
  • 收藏
  • 关注

原创 2021-08-06 | 337. 打家劫舍 III 【二叉树跨层求路径最大和】

# 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 = rightclass Solution: def rob(self, root: TreeNode) -> int: .

2021-08-21 21:32:57 225

原创 每日一道Leetcode - 74. 搜索二维矩阵

class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: if not matrix or not matrix[0]: return False m = len(matrix) n = len(matrix[0]) i = m-1 j = 0 while 0&.

2021-08-06 10:51:05 199

原创 每日一道Leetcode - 560. 和为K的子数组 【前缀和+字典】

class Solution: def subarraySum(self, nums: List[int], k: int) -> int: # 前缀和字典,当前前缀和:次数; 前缀和为0的数组出现次数为1次 preSum = {} preSum[0] = 1 count = 0 pre = 0 for num in nums: pre += num .

2021-08-05 12:00:47 205

原创 每日一道Leetcode - 204. 计数质数

class Solution: def countPrimes(self, n: int) -> int: isPrime = [True]*n for i in range(2,n): if i*i < n: if isPrime[i]: # 当前是质数 for j in range(i*i,n,i): .

2021-08-05 10:42:31 77

原创 每日一道Leetcode - 257. 二叉树的所有路径 [DFS]

# 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 = rightclass Solution: def binaryTreePaths(self, root: TreeNode) -.

2021-08-04 10:20:40 56

原创 每日一道Leetcode - 174. 地下城游戏 [反向动态规划|路径]

class Solution: def calculateMinimumHP(self, dungeon: List[List[int]]) -> int: m = len(dungeon) n = len(dungeon[0]) dp = [[0]* n for _ in range(m)] for i in range(m-1,-1,-1): for j in range(n-1,-1,-1): .

2021-08-03 17:16:51 83

原创 每日一道Leetcode - 93. 复原 IP 地址 【回溯】

class Solution: def restoreIpAddresses(self, s: str) -> List[str]: # 判断当前字段是否合法 def judge_ip_segment(s,left,right): # 当前ip段的大小 size = right-left + 1 # 长度大于1的情况下,当前段ip起始不能为0 if size &gt.

2021-07-18 12:01:12 47

原创 每日一道Leetcode - 220. 存在重复元素 III 【桶排序】

class Solution: def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool: # 使用桶排序 # 比如说,在一个班上,日期相差30天以内的 # 一个桶内的元素都是在t的范围内的 def getIdx(num): # 获得当前数据的桶id if num < 0:.

2021-07-17 16:35:57 79

原创 每日一道Leetcode - 面试题 17.09. 第 k 个数 [动态规划|三指针]

class Solution: def getKthMagicNumber(self, k: int) -> int: dp = [0]*(k+1) dp[0] = 1 # 三个指针分别指向三个列表头【第一个是以3为倍数的列表,第二个是以5为倍数的列表,第三个是以7为倍数的列表,在这三个数字中找最小的,再滑动指针】 l1,l2,l3 = 0,0,0 for i in range(1,k): .

2021-07-07 10:39:52 98

原创 每日一道Leetcode - 面试题 17.10. 主要元素 【摩尔投票法】

class Solution: def majorityElement(self, nums: List[int]) -> int: # 使用摩尔投票法 count = 0 candidate = 0 for num in nums: if count == 0: #当前没有候选人拉 candidate = num .

2021-07-06 10:59:43 65

原创 每日一道Leetcode -面试题 17.11. 单词距离 [字典|双指针]

class Solution: def findClosest(self, words: List[str], word1: str, word2: str) -> int: # 字典存储每个词的出现的所有位置 dic = {} for i,word in enumerate(words): if word not in dic: dic[word] = [] di.

2021-07-06 10:46:18 121

原创 每日一道Leetcode -面试题 17.13. 恢复空格 【动态规划|字典树】

class TrieNode: def __init__(self): self.isWord = False self.children = [None for _ in range(26)]class Trie: def __init__(self): self.root = TrieNode() # 将dic中的单词倒序插入字典树 def insert(self,word): cur = self.ro.

2021-07-04 12:22:25 81

原创 每日一道Leetcode-面试题 17.14. 最小K个数【快速排序】

class Solution: def partition(self,nums,l,r): # pivot存在最右边 """ 3 1 5 7 4 i = -1 j=0: 3 < 4: i = -1+1 = 0 nums[0] = 3 nums[0] = 3 j=1: 1 < 4: i = 0+1 = 1 nums[1] = 1 nums[1] = 1 j=2: 5 > .

2021-07-03 12:45:55 129

原创 每日一道Leetcode - 面试题 17.17. 多次搜索 【字典树】

class Trie: # 构建字典树 def __init__(self,words): self.d = {} for word in words: t = self.d for w in word: if w not in t: t[w] = {} t = t[w] t['end.

2021-07-01 10:28:26 118

原创 每日一道Leetcode - 面试题 17.18. 最短超串 [滑动窗口]

class Solution: def shortestSeq(self, big: List[int], small: List[int]) -> List[int]: value = N_small = len(small) N_big = len(big) dic = dict() for num in small: if num not in dic: dic[nu.

2021-06-30 10:50:32 96

原创 每日一道Leetcode - 面试题 17.22. 单词转换

class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[str]: def change_only_first_element(first, second): if len(first) != len(second): return False count = .

2021-06-27 16:45:18 119

原创 每日一道Leetcode - 面试题 17.23. 最大黑方阵

class Solution: def findSquare(self, matrix: List[List[int]]) -> List[int]: # 从右下角开始遍历,记录当前元素下边和右边的0的元素个数 N = len(matrix) M = len(matrix[0]) if N == 0: return [] if N == 1: if matrix[0.

2021-06-26 14:54:20 82

原创 每日一道Leetcode - 面试题 17.24. 最大子矩阵【动态规划】

class Solution: def getMaxMatrix(self, matrix: List[List[int]]) -> List[int]: ans = [0,0,0,0] N = len(matrix) M = len(matrix[0]) res = float('-inf') # 上边为i,下边为j,列使用k进行遍历 for i in range(0,N): .

2021-06-26 12:22:47 103

原创 每日一道Leetcode - 剑指 Offer 43. 1~n 整数中 1 出现的次数

class Solution: def countDigitOne(self, n: int) -> int: """ case 1: cur=0 2 3 0 4 千位和百位可以选00 01 02....22 十位可以取到1( 形如[00|01..|22]1[0-9] 都是<2304 ) 个位可以选0-9 共有 23 * 10 中排列 当千位和百位取23,如果十位取1 那就是形如 231[0-9] > 23.

2021-06-20 15:49:08 53

原创 每日一道Leetcode -剑指 Offer 44. 数字序列中某一位的数字

class Solution: def findNthDigit(self, n: int) -> int: digit, start, count = 1,1,9 while n > count: n -= count start *= 10 digit += 1 count = 9 * start * digit num = start +.

2021-06-16 22:31:22 69

原创 每日一道Leetcode- 剑指 Offer 46. 把数字翻译成字符串 【动态规划】

class Solution: def translateNum(self, num: int) -> int: """ 单独一个:dp[i] = dp[i-1] 两个一起需要满足 这两个数字在[10,25]之间,dp[i] = dp[i-1]+dp[i-2] """ # a,b 为 dp[i-2] dp[i-1], 初始分别表示 dp[0],dp[1] a=b=1 # 先转为字符.

2021-06-13 14:09:12 68

原创 每日一道Leetcode - 剑指 Offer 47. 礼物的最大价值 【动态规划】

class Solution: def maxValue(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) dp = [[0]*(n) for _ in range(m)] for i in range(m): for j in range(n): if i==0 and j==0: .

2021-06-13 13:49:14 34

原创 每日一道Leetcode - 剑指 Offer 48. 最长不含重复字符的子字符串 【滑动窗口】

class Solution: def lengthOfLongestSubstring(self, s: str) -> int: # 滑动窗口 n = len(s) if n==0: return 0 left = 0 right = 0 # 存储每个字符最后出现的位置 cur = {} ans = 0 while rig.

2021-06-12 20:46:39 65

原创 每日一道Leetcode - 剑指 Offer 49. 丑数 【动态规划】

class Solution: def nthUglyNumber(self, n: int) -> int: # 动态规划 dp = [0] * (n) dp[0] = 1 a,b,c = 0,0,0 for i in range(1,n): dp[i] = min(dp[a]*2,dp[b]*3,dp[c]*5) if dp[i] == dp[a]*2: .

2021-06-12 20:22:13 71

原创 剑指 Offer 50. 第一个只出现一次的字符 【哈希表】

class Solution: def firstUniqChar(self, s: str) -> str: # O(n) O(1) dic = dict() for c in s: if c not in dic: dic[c] = 0 dic[c]+=1 for k,v in dic.items(): if v == 1: .

2021-06-11 15:02:14 36

原创 每日一道Leetcode - 剑指 Offer 51. 数组中的逆序对 【归并排序】

class Solution: def reversePairs(self, nums: List[int]) -> int: # 时间复杂度:归并排序 O(NlogN) # 空间复杂度 O(N) def mergeSort(nums,left,mid,right): # 整合的数组 temp = [0] * (right-left+1) # 记录更新temp数组的下标 .

2021-06-11 14:43:22 59

原创 每日一道Leetcode - 剑指 Offer 52. 两个链表的第一个公共节点

# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: """ .

2021-06-10 12:09:58 46

原创 每日一道Leetcode -剑指 Offer 53 - I. 在排序数组中查找数字 I 【两次二分查找目标值所在的区间】

class Solution: def search(self, nums: List[int], target: int) -> int: """ 找左右区间 """ i = 0 j = len(nums)-1 # 先找右边界吧 while i<=j: mid = i + (j-i)//2 if nums[mid] > .

2021-06-10 12:02:55 53

原创 每日一道Leetcode - 剑指 Offer 53 - II. 0~n-1中缺失的数字 【二分查找】

class Solution: def missingNumber(self, nums: List[int]) -> int: """ 排序数组: 排序数组中的搜索问题,首先想到 二分法 解决 O(logN) 分为左右两半数组 左半部分满足 nums[i] = i 右半部分满足 nums[i] 不等于 i 二分查找区间 0 1 3 缺2.

2021-06-09 10:47:33 39

原创 每日一道Leetcode - 剑指 Offer 54. 二叉搜索树的第k大节点 【中序遍历的倒序】

# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def kthLargest(self, root: TreeNode, k: int) -> int: """ .

2021-06-09 10:27:20 67

原创 每日一道Leetcode - 剑指 Offer 55 - I. 二叉树的深度/平衡二叉树

# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def maxDepth(self, root: TreeNode) -> int: """ # 使用DF.

2021-06-08 10:41:18 52

原创 每日一道Leetcode - 剑指 Offer 56 - I. 数组中数字出现的次数 【异或】

class Solution: def singleNumbers(self, nums: List[int]) -> List[int]: """ 对所有数字进行异或操作,最后的异或值操作一定是两个只出现一次的值 functools.reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) function 为 x + y,sequence 为 [1, 2, 3, 4, 5],未给出 initial,.

2021-06-07 11:47:18 56

原创 每日一道Leetcode - 剑指 Offer 57. 和为s的两个数字 【双指针|滑动窗口】

class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: n = len(nums) i = 0 j = n-1 while i<n and j>0: if nums[i]+nums[j]>target: j -= 1 elif nums.

2021-06-06 10:56:48 46

原创 每日一道Leetcode - 剑指 Offer 58 - I. 翻转单词顺序 【字符串】

class Solution: def reverseWords(self, s: str) -> str: """ split()的时候,多个空格当成一个空格; split(' ')的时候,多个空格都要分割,每个空格分割出来空。 """ # 去除首尾空格 s = s.strip() # 按空格分隔 s = s.split() # 列表反转 .

2021-06-05 10:56:55 52

原创 每日一道Leetcode - 剑指 Offer 58 - II. 左旋转字符串 [切片]

class Solution: def reverseLeftWords(self, s: str, n: int) -> str: return s[n:]+s[:n]

2021-06-05 10:48:41 56

原创 每日一道Leetcode - 剑指 Offer 59 - I. 滑动窗口的最大值 【滑动窗口|双端队列存最大值】

class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: n = len(nums) if n==0: return [] ans = [] # 定义一个双端队列,队首存储最大值索引 q = collections.deque() # 先存k个元素到双端队列中,每存一个值要判断一下最大值 .

2021-06-04 11:43:52 62

原创 每日一道Leetcode - 剑指 Offer 59 - II. 队列的最大值【模拟|双栈】

class MaxQueue: def __init__(self): self.A = [] self.B = [] def max_value(self) -> int: if not self.B: return -1 return self.B[0] def push_back(self, value: int) -> None: while self.

2021-06-04 11:28:00 51

原创 每日一道Leetcode - 剑指 Offer 60. n个骰子的点数【动态规划】

class Solution: def dicesProbability(self, n: int) -> List[float]: """ 分析问题:2枚骰子的情况 第一枚可能情况:1,2,3,4,5,6 【已知情况】 第二枚可能情况(在第一枚的基础上):1+[1-6],2+[1-6]..6+[1-6] 如果想要在2枚骰子的情况下得到点数为4: 那么第二枚骰子(第一枚情况,第二枚情况) = (1.

2021-06-03 11:03:27 67

原创 每日一道Leetcode - 剑指 Offer 61. 扑克牌中的顺子 【排序|最大牌-最小牌<5】

class Solution: def isStraight(self, nums: List[int]) -> bool: """ 1.对数组排序!!! 2.统计多少个大小王【遍历】 大小王个数jokerjoker对应的nums[joker]所在位置应该是最小牌的数字 3.数组的最后一个则是最大牌的数字 nums[4] 4.条件1: 确保最大牌-最小牌小于5 nums[4]-nums[joker.

2021-06-03 10:11:06 130

原创 每日一道Leetcode - 剑指 Offer 62. 圆圈中最后剩下的数字 【约瑟夫环|递归|反推】

class Solution: def lastRemaining(self, n: int, m: int) -> int: """ 最终剩下一个人时的安全位置肯定为0,反推安全位置在人数为n时的编号 人数为1: 0 人数为2: (0+m) % 2 人数为3: ((0+m) % 2 + m) % 3 人数为4: (((0+m) % 2 + m) % 3 + m) % 4 .....

2021-06-02 10:48:29 77

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除