![](https://img-blog.csdnimg.cn/20190918140129601.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
LeetCode
leetcode每日三题
Leadingme
干啥啥不行,写bug第一名!
展开
-
leetcode-5.27[541. 反转字符串 II、345. 反转字符串中的元音字母、557. 反转字符串中的单词 III](python实现)
题目1题解1class Solution: def reverseStr(self, s: str, k: int) -> str: res='' for i in range(0,len(s),2*k): res+=s[i:i+k][::-1]+s[i+k:i+2*k] return res附上题链接题目2题解2class Solution: def reverseVowels(self,原创 2020-05-27 12:05:24 · 310 阅读 · 0 评论 -
leetcode-5.25[190. 颠倒二进制位](python实现)
题目1题解1class Solution: def reverseBits(self, n: int) -> int: ans = 0 i = 32 # 循环移动32次 while i > 0: # 向左移动 ans <<= 1 # 移动后将末尾加n的尾部值 ans += n&1原创 2020-05-25 11:24:18 · 278 阅读 · 0 评论 -
leetcode-5.24[*671. 二叉树中第二小的节点、728. 自除数、645. 错误的集合](python实现)
题目1题解1# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: """ 每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么这个节点原创 2020-05-24 13:46:13 · 283 阅读 · 0 评论 -
leetcode-5.22[*748. 最短完整词、653. 两数之和 IV - 输入 BST、821. 字符的最短距离](python实现)
题目1题解1class Solution: def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str: s = [w for w in licensePlate.lower() if ord(w)>=97 and ord(w)<=122] words.sort(key=lambda x:len(x)) #按单词长度进行排序 for原创 2020-05-22 13:36:34 · 257 阅读 · 0 评论 -
leetcode-5.21[581. 最短无序连续子数组、905. 按奇偶排序数组、922. 按奇偶排序数组 II](python实现)
题目1题解1class Solution: def findUnsortedSubarray(self, nums: List[int]) -> int: l,r = 0, len(nums)-1 while l <= r: Max = max(nums[l:r+1]) Min = min(nums[l:r+1]) if nums[l] == Min:原创 2020-05-21 11:25:00 · 207 阅读 · 0 评论 -
leetcode-5.20[1356. 根据数字二进制下 1 的数目排序、977. 有序数组的平方、1221. 分割平衡字符串](python实现)
题目1题解1class Solution: def sortByBits(self, arr: List[int]) -> List[int]: res = [] d = {} for i in arr: n = self.oneCount(i) if n not in d: d[n] =[] d[n].append(i)原创 2020-05-20 11:58:45 · 189 阅读 · 0 评论 -
leetcode-5.19[1281. 整数的各位积和之差、811. 子域名访问计数、551. 学生出勤记录 I](python实现)
题目1题解1class Solution: def subtractProductAndSum(self, n: int) -> int: cumsum = 0 cummul = 1 while n: temp = n%10 cumsum += temp cummul *= temp n = n//10 return cummul原创 2020-05-19 12:26:44 · 190 阅读 · 0 评论 -
leetcode-5.18[985. 查询后的偶数和、404. 左叶子之和](python实现)
题目1题解1class Solution: def sumEvenAfterQueries(self, A: List[int], queries: List[List[int]]) -> List[int]: S = sum(item for item in A if item%2==0) sum_even = [] for item in queries: index = item[1]原创 2020-05-18 11:19:49 · 139 阅读 · 0 评论 -
leetcode-5.16[1022. 从根到叶的二进制数之和、633. 平方数之和、371. 两整数之和](python实现)
题目1题解1# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # def sumRootToLeaf(self, root: TreeNode) -> int: # "原创 2020-05-16 12:52:16 · 177 阅读 · 0 评论 -
leetcode-5.15[560. 和为K的子数组、1337. 方阵中战斗力最弱的 K 行](python实现)
题目1题解1class Solution: def subarraySum(self, nums: List[int], k: int) -> int: hash={0:1} sum=0 count=0 for i in range(len(nums)): sum+=nums[i] if (sum-k) in hash: count+=hash[原创 2020-05-15 10:11:02 · 200 阅读 · 0 评论 -
leetcode-5.14[414. 第三大的数、709. 转换成小写字母、766. 托普利茨矩阵](python实现)
题目1题解1class Solution: def thirdMax(self, nums: List[int]) -> int: # 集合去重 nums = list(set(nums)) if len(nums) <= 2: return max(nums) # 三次选择排序O(3n) -> O(n) for i in range(3): max原创 2020-05-14 11:38:25 · 182 阅读 · 1 评论 -
leetcode-5.13[867. 转置矩阵、669. 修剪二叉搜索树、1029. 两地调度](python实现)
题目1题解1class Solution: def transpose(self, A: List[List[int]]) -> List[List[int]]: n = len(A[0]) m = len(A) B = [[None for _ in range(len(A))]for _ in range(len(A[0]))] print(B) for i in range(m):原创 2020-05-13 12:49:59 · 175 阅读 · 0 评论 -
leetcode-5.12[263. 丑数、884. 两句话中的不常见单词、929. 独特的电子邮件地址](python实现)
题目1题解1class Solution: def isUgly(self, num: int) -> bool: """ 贪心算法 """ # if num < 1: # return False # while num % 2 == 0: # num /= 2 # while num %原创 2020-05-12 11:37:32 · 399 阅读 · 0 评论 -
leetcode-5.11[804. 唯一摩尔斯密码词、1207. 独一无二的出现次数](python实现)
题目1题解1class Solution: def uniqueMorseRepresentations(self, words: List[str]) -> int: Morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-原创 2020-05-11 12:16:49 · 209 阅读 · 0 评论 -
leetcode-5.10[965. 单值二叉树、242. 有效的字母异位词、*236. 二叉树的最近公共祖先](python实现)
题目1题解1# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def isUnivalTree(self, root: TreeNode) -> bool: if ro原创 2020-05-10 11:28:30 · 155 阅读 · 0 评论 -
leetcode-5.9[680. 验证回文字符串 II、941. 有效的山脉数组、1037. 有效的回旋镖](python实现)
题目1题解1class Solution: def validPalindrome(self, s: str) -> bool: l, r = 0, len(s) - 1 while l < r: if s[l] != s[r]: case1, case2 = s[l:r], s[l+1:r+1] # 如果原数组去头,去尾的两个数组有一个为回文,则整体为回文原创 2020-05-09 17:00:44 · 155 阅读 · 0 评论 -
leetcode-5.8[367. 有效的完全平方数、290. 单词规律、914. 卡牌分组](python实现)
题目1题解1class Solution: def isPerfectSquare(self, num: int) -> bool: """ 二分查找 """ if num < 2: return num # 因为除了1以外的所有有效数的完全平方一定小于等于nu...原创 2020-05-08 11:36:01 · 155 阅读 · 0 评论 -
leetcode-5.7[LCP 06. 拿硬币、572. 另一个树的子树、LCP 07. 传递信息](python实现)
题目1题解1class Solution: def minCount(self, coins: List[int]) -> int: """ 贪心算法 """ count = 0 for coin in coins: num, mod = divmod(coin,2) ...原创 2020-05-07 12:15:53 · 226 阅读 · 0 评论 -
leetcode-5.6[219. 存在重复元素 II、225. 用队列实现栈、226. 翻转二叉树](python实现)
题目1题解1class Solution: def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: seen = list() n = len(nums) i = 0 while i < n: if num...原创 2020-05-06 10:38:07 · 184 阅读 · 0 评论 -
leetcode-5.5[217. 存在重复元素、205. 同构字符串、204. 计数质数](python实现)
题目1题解1class Solution: def containsDuplicate(self, nums: List[int]) -> bool: seen = set() i = 0 n = len(nums) while i < n: if nums[i] not in se...原创 2020-05-05 10:42:01 · 174 阅读 · 0 评论 -
leetcode-5.4[191. 位1的个数、202. 快乐数、203. 移除链表元素](python实现)
题目1题解1class Solution: def hammingWeight(self, n: int) -> int: sum = 0 # 每次减去一个1,然后与原值相与,得到的值会少一个1 while n != 0: sum += 1 n &= (n-1) ...原创 2020-05-04 11:38:40 · 175 阅读 · 0 评论 -
leetcode-5.3[172. 阶乘后的零、189. 旋转数组、5400. 旅行终点站](python实现)
题目1题解1class Solution: def trailingZeroes(self, n: int) -> int: """ 末尾的0来自因子2和5 ,只有有5,就一定存在一个数可以拆成2(4,6,8)乘以某个数。 这样末尾就有0了,所以只用找到0的个数 """ times ...原创 2020-05-03 11:43:07 · 173 阅读 · 0 评论 -
leetcode-5.2[171. Excel表列序号、168. Excel表列名称、167. 两数之和 II - 输入有序数组](python实现)
题目1题解1class Solution: def titleToNumber(self, s: str) -> int: res = 0 for index, word in enumerate(s, start=1): if index < len(s): res = (res +...原创 2020-05-02 13:22:00 · 171 阅读 · 0 评论 -
leetcode-5.1[136. 只出现一次的数字、122. 买卖股票的最佳时机 II、125. 验证回文串](python实现)
题目1题解1class Solution: """ 时间复杂度: O(n^2) 空间复杂度: O(n) """ def singleNumber(self, nums: List[int]) -> int: temp = [] for num in nums: if num ...原创 2020-05-01 11:46:02 · 316 阅读 · 0 评论 -
leetcode-4.30[112. 路径总和、118. 杨辉三角、121. 买卖股票的最佳时机](python实现)
题目1题解1# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def hasP...原创 2020-04-30 10:01:23 · 236 阅读 · 0 评论 -
leetcode-4.29[111. 二叉树的最小深度、108. 将有序数组转换为二叉搜索树、110. 平衡二叉树](python解法)
题目1题解1# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def minD...原创 2020-04-29 11:49:34 · 157 阅读 · 0 评论 -
leetcode-4.28[107. 二叉树的层次遍历 II、101. 对称二叉树、104. 二叉树的最大深度](python解法)
题目1题解1# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def leve...原创 2020-04-28 11:56:50 · 191 阅读 · 1 评论 -
leetcode-4.27[83. 删除排序链表中的重复元素、100. 相同的树](python解法)
题目1题解1# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def deleteDuplicates(self, head: Lis...原创 2020-04-27 18:38:21 · 142 阅读 · 2 评论 -
leetcode-4.26[70. 爬楼梯、69. x 的平方根、67. 二进制求和](python解法)
题目1题解1class Solution: """ 1. 子问题: DP[i] 到第i元素最多的爬取方式 2. 子方法之间的关系: 第 i 阶可以由以下两种方法得到: 在第 (i-1)阶后向上爬1阶。 在第 (i-2)阶后向上爬2阶 DP[i] = DP[...原创 2020-04-26 11:07:03 · 221 阅读 · 0 评论 -
leetcode-4.25[66. 加一、58. 最后一个单词的长度、53. 最大子序和](python解法)
题目1题解1class Solution: def plusOne(self, digits: List[int]) -> List[int]: n = len(digits)-1 digits[n] += 1 while n >= 0: # 如果第一个数为10,则必须在首位前添加一个元素 ...原创 2020-04-25 11:50:51 · 147 阅读 · 0 评论 -
leetcode-4.24[35. 搜索插入位置、28. 实现 strStr()、27. 移除元素](python解法)
题目1题解1class Solution: def searchInsert(self, nums: List[int], target: int) -> int: for index, num in enumerate(nums): if target <= num: return index ...原创 2020-04-24 10:15:15 · 142 阅读 · 0 评论 -
leetcode-4.23[7. 整数反转、9. 回文数、13. 罗马数字转整数](python解法)
题目1题解1class Solution: def reverse(self, x: int) -> int: num, res = abs(x), 0 of = (1 << 31) - 1 if x > 0 else 1 << 31 # 初始一个值存最终结果,每次取出num,取出最小位上的值并把前面...原创 2020-04-23 12:55:05 · 175 阅读 · 0 评论 -
leetcode-4.22[215. 数组中的第K个最大元素、206. 反转链表、198. 打家劫舍](python解法)
题目1题目2class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: # 递归实现快速排序 def quickSort(nums): if len(nums) <= 1: return nums...原创 2020-04-22 12:11:46 · 147 阅读 · 0 评论 -
leetcode-4.21[169. 多数元素、160. 相交链表、155. 最小栈](python解法)
题目1题解1from queue import PriorityQueueclass Solution: def majorityElement(self, nums: List[int]) -> int: pq = PriorityQueue(maxsize=0) for num in set(nums): pq.pu...原创 2020-04-21 11:20:15 · 129 阅读 · 0 评论 -
leetcode-4.20[148. 排序链表、147. 对链表进行插入排序、146. LRU缓存机制](python解法)
题目1题解1# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Nonefrom queue import PriorityQueueclass Solution: """...原创 2020-04-20 12:24:26 · 165 阅读 · 0 评论 -
leetcode-4.19[143. 重排链表、141. 环形链表、142. 环形链表 II](python解法)
题目1题解1# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def reorderList(self, head: ListNode...原创 2020-04-19 13:40:06 · 199 阅读 · 0 评论 -
leetcode-4.18[606. 根据二叉树创建字符串、11. 盛最多水的容器、1310. 子数组异或查询](python解法)
题目1题解1# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def tree...原创 2020-04-18 11:36:32 · 172 阅读 · 0 评论 -
leetcode-4.17[819. 最常见的单词、1108. IP 地址无效化、55. 跳跃游戏](python解法)
题目1题解1from queue import PriorityQueueclass Solution: def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: pq = PriorityQueue(maxsize=0) for c in "!?',;.": ...原创 2020-04-17 12:52:43 · 150 阅读 · 0 评论 -
leetcode-4.16[56. 合并区间、面试题 04.04. 检查平衡性、700. 二叉搜索树中的搜索](python解法)
题目1题解1class Solution: def merge(self, intervals: List[List[int]]) -> List[List[int]]: intervals.sort(key = lambda x: x[0]) # 根据左区间从小到大排序 res = [] for i in range(len(...原创 2020-04-16 13:52:34 · 211 阅读 · 0 评论 -
leetcode-4.15[105. 从前序与中序遍历序列构造二叉树、1344. 时钟指针的夹角、908. 最小差值 I](python解法)
题目1解法1:# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def buil...原创 2020-04-15 12:59:54 · 162 阅读 · 0 评论