- 博客(35)
- 收藏
- 关注
原创 【leetcode-python-35】437. 路径总和 III
【leetcode-python-35】437. 路径总和 III参考大佬版1(49.94%)参考大佬版2(82.23%)leetcode 437. 路径总和 III参考大佬版1(49.94%)双层递归。pathSum记录路径数=以根节点开始的路径数+以左右子树为整体的路径数(不一定以左右子树的根节点开始)。dfs记录以根节点开始的路径数,只要找到一条就+1,找不到就递归地-root.val往下继续找。这样每一条都找过且都得算,可能有重复的,太慢了。那就递归+散列表,会更高效,具体代码请参考题解
2020-06-20 12:49:59
177
原创 【leetcode-python-34】面试题 04.04. 检查平衡性
【leetcode-python-34】面试题 04.04. 检查平衡性参考大佬版1(5.46%)参考大佬版2(64.71%)leetcode 面试题 04.04. 检查平衡性参考大佬版1(5.46%)用check_height函数返回子树高度。在主函数中判断高度差,若相差大于1,则返回False。如果左右子树高度差相差小于等于1,则再看左右子树是否同时为平衡树。注:参考题解区疾风知劲草思路。class Solution(object): def isBalanced(self, root
2020-06-19 21:34:22
261
原创 【leetcode-python-33-递归】面试题10- II. 青蛙跳台阶问题
【leetcode-python-33】面试题10- II. 青蛙跳台阶问题渣渣原始版(85.62%)leetcode 面试题10- II. 青蛙跳台阶问题渣渣原始版(85.62%)总方法数 = 跳最后一阶楼梯方法数 + 跳最后两阶楼梯方法数class Solution(object): def numWays(self, n): """ :type n: int :rtype: int """ a, b = 0
2020-06-11 22:51:49
711
原创 【leetcode-python-32-递归】面试题 08.06. 汉诺塔问题
【leetcode-python-32-递归】面试题 08.06. 汉诺塔问题参考大佬版(86%)leetcode 面试题 08.06. 汉诺塔问题参考大佬版(86%)递归。注:参考题解区腐烂的橘子????思路。class Solution(object): def hanota(self, A, B, C): """ :type A: List[int] :type B: List[int] :type C: List[in
2020-06-11 20:53:09
231
原创 【leetcode-python-31】面试题10- I. 斐波那契数列
【leetcode-python-31】面试题10- I. 斐波那契数列渣渣原始版(62.68%)大佬版(99.68%)leetcode 面试题10- I. 斐波那契数列渣渣原始版(62.68%)如果只对最后结果求余,是37.08%。先求余后相加再求余,和全部加起来再求余是一样的效果。class Solution(object): def fib(self, n): """ :type n: int :rtype: int "
2020-06-11 17:39:50
210
原创 【leetcode-python-30】1137. 第 N 个泰波那契数
【leetcode-python-30】1137. 第 N 个泰波那契数渣渣原始版(100%)leetcode 1137. 第 N 个泰波那契数渣渣原始版(100%)我的第一个双百分,感动!!我是用的双端队列,很方便的。class Solution(object): def tribonacci(self, n): """ :type n: int :rtype: int """ import collecti
2020-06-11 16:47:57
227
原创 【leetcode-python-29】面试题 16.11. 跳水板
【leetcode-python-29】面试题 16.11. 跳水板渣渣原始版(85.71%)leetcode 面试题 16.11. 跳水板渣渣原始版(85.71%)直接用range方法。class Solution(object): def divingBoard(self, shorter, longer, k): """ :type shorter: int :type longer: int :type k: int
2020-06-11 16:13:23
1284
原创 【leetcode-python-28】121. 买卖股票的最佳时机
【leetcode-python-28】121. 买卖股票的最佳时机渣渣原始版(83.90%)参考官方版(94.46%)leetcode 121. 买卖股票的最佳时机渣渣原始版(83.90%)考虑的是最高点卖出,用了reverse(),不是很好。以后遇到“最大+inserve”,可以考虑“最小”。class Solution(object): def maxProfit(self, prices): """ :type prices: List[int]
2020-06-11 15:27:00
235
原创 【leetcode-python-27】122. 买卖股票的最佳时机 II
【leetcode-python-27】122. 买卖股票的最佳时机 II渣渣原始版(44.28%)渣渣原始版(53.05%)leetcode 122. 买卖股票的最佳时机 II渣渣原始版(44.28%)每天都决定一下买卖与否。class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """
2020-06-11 15:12:12
168
原创 【leetcode-python-26】1005. K 次取反后最大化的数组和
【leetcode-python-26】1005. K 次取反后最大化的数组和渣渣原始版(86.27%)渣渣改进版(95.10%)leetcode 1005. K 次取反后最大化的数组和渣渣原始版(86.27%)也是先排序,再看原本最小值的正负情况。class Solution(object): def largestSumAfterKNegations(self, A, K): """ :type A: List[int] :type K:
2020-06-11 14:13:32
200
1
原创 【leetcode-python-25】455. 分发饼干
【leetcode-python-25】455. 分发饼干渣渣原始版(88.51%)leetcode 455. 分发饼干渣渣原始版(88.51%)双指针。class Solution(object): def findContentChildren(self, g, s): """ :type g: List[int] :type s: List[int] :rtype: int """ num
2020-06-10 23:27:20
179
原创 【leetcode-python-24】1046. 最后一块石头的重量
【leetcode-python-24】1046. 最后一块石头的重量渣渣原始版(59.09%)参考大佬版(59.09%)leetcode 1046. 最后一块石头的重量渣渣原始版(59.09%)先排序然后再一个一个比对。class Solution(object): def lastStoneWeight(self, stones): """ :type stones: List[int] :rtype: int """
2020-06-10 21:30:49
252
原创 【leetcode-python-23】1221. 分割平衡字符串
【leetcode-python-23】1221. 分割平衡字符串渣渣原始版(92.73%)leetcode 1221. 分割平衡字符串渣渣原始版(92.73%)class Solution(object): def balancedStringSplit(self, s): """ :type s: str :rtype: int """ if not s: return 0
2020-06-10 18:57:56
191
原创 【leetcode-python-22】874. 模拟行走机器人
【leetcode-python-22】874. 模拟行走机器人渣渣原始版(超时啦!)官方版(57.64%)leetcode 874. 模拟行走机器人渣渣原始版(超时啦!)class Solution(object): def robotSim(self, commands, obstacles): """ :type commands: List[int] :type obstacles: List[List[int]] :rt
2020-06-10 14:03:33
186
原创 【leetcode-python-21】860. 柠檬水找零
【leetcode-python-21】860. 柠檬水找零渣渣原始版(98.48%)leetcode 860. 柠檬水找零渣渣原始版(98.48%)因为只有三种货币,可以直接计数。class Solution(object): def lemonadeChange(self, bills): """ :type bills: List[int] :rtype: bool """ if bills[0] !=
2020-06-09 19:26:14
151
原创 【leetcode-python-20】1029. 两地调度
【leetcode-python-20】1029. 两地调度渣渣原始版(90.91%)参考官方版(55.30%)leetcode 1029. 两地调度渣渣原始版(90.91%)先假设都飞B,根据差值排序,然后将小的分配到A。class Solution(object): def twoCitySchedCost(self, costs): """ :type costs: List[List[int]] :rtype: int
2020-06-09 17:17:07
148
原创 【leetcode-python-19】1403. 非递增顺序的最小子序列
【leetcode-python-19】1403. 非递增顺序的最小子序列渣渣原始版(77.58%)leetcode 1403. 非递增顺序的最小子序列渣渣原始版(77.58%)先排序class Solution(object): def minSubsequence(self, nums): """ :type nums: List[int] :rtype: List[int] """ a = sorted(
2020-06-09 16:25:34
156
原创 【leetcode-python-18】1217. 玩筹码
【leetcode-python-18】1217. 玩筹码渣渣原始版(54.84%)leetcode 1217. 玩筹码渣渣原始版(54.84%)class Solution(object): def minCostToMoveChips(self, chips): """ :type chips: List[int] :rtype: int """ x = 0 y = 0 for
2020-06-09 15:21:09
329
原创 【leetcode-python-17】944. 删列造序
【leetcode-python-17】944. 删列造序参考官方版1(60.81%)参考官方版2(87.84%)leetcode 944. 删列造序参考官方版1(60.81%)渣渣原始版没有用zip(*A),而是先把它变为list再比较。O(N^2)超出时间限制了。没有用break,而是用的any。官方还用到了xrange:函数用法与 range 完全相同,所不同的是生成的不是一个数组,而是一个生成器。注:参考题解区官方思路。class Solution(object): de
2020-06-08 20:53:36
124
原创 【leetcode-python-16】392. 判断子序列
【leetcode-python-16】392. 判断子序列渣渣原始版(98.84%)leetcode 392. 判断子序列渣渣原始版(98.84%)像是双指针吧。class Solution(object): def isSubsequence(self, s, t): """ :type s: str :type t: str :rtype: bool """ if not s:
2020-06-08 19:38:57
209
原创 【leetcode-python-15-链表】面试题25. 合并两个排序的链表
【leetcode-python-15-链表】面试题25. 合并两个排序的链表参考大佬版(93.61%)leetcode 面试题09. 用两个栈实现队列参考大佬版(93.61%)x1为头,x2为尾。注:参考题解区Krahets思路。class Solution(object): def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode
2020-06-08 19:00:46
115
原创 【leetcode-python-14-动态规划】面试题 16.17. 连续数列
【leetcode-python-14】面试题 16.17. 连续数列渣渣原始版(70.23%)参考大佬版(暂未编辑)leetcode 面试题 16.17. 连续数列渣渣原始版(70.23%)class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ for i in nums:
2020-06-07 23:30:37
168
原创 【leetcode-python-13-分治算法】面试题39. 数组中出现次数超过一半的数字
【leetcode-python-13-分治算法】面试题39. 数组中出现次数超过一半的数字参考大佬版1(53.01%)参考大佬版(68.08%)leetcode 面试题39. 数组中出现次数超过一半的数字参考大佬版1(53.01%)摩尔投票法: 核心理念为 “正负抵消” 。票数和:由于众数出现的次数超过数组长度的一半;若记 众数 的票数为+1,非众数 的票数为−1,则一定有所有数字的 票数和 >0。注:参考题解区Krahets思路。class Solution(object):
2020-06-07 19:05:54
238
原创 【leetcode-python-12-栈】933. 最近的请求次数
【leetcode-python-12】933. 最近的请求次数渣渣原始版(54.17%)参考官方版(90%)参考官方版(95%)leetcode 933. 最近的请求次数渣渣原始版(54.17%)用的list,且用了n作为计数。class RecentCounter(object): def __init__(self): self.items = [] self.n = 0 def ping(self, t):
2020-06-07 18:04:49
140
原创 【leetcode-python-11】1021. 删除最外层的括号
【leetcode-python-11】1021. 删除最外层的括号渣渣原始版(68.56%)渣渣改进版(84.95%)参考大佬版(93.98%)leetcode 1021. 删除最外层的括号渣渣原始版(68.56%)用一个n来判断是否右括号append完。用了list。class Solution(object): def removeOuterParentheses(self, S): """ :type S: str :rtype: st
2020-06-07 17:11:21
199
原创 【leetcode-python-10】844. 比较含退格的字符串
【leetcode-python-1】844. 比较含退格的字符串渣渣原始版(49.28%)官方版(72.73%)leetcode 844. 比较含退格的字符串渣渣原始版(49.28%)最原始的思路了。class CQueue1(object): def __init__(self): self.items1 = [] self.items2 = [] def appendTail(self, value): """
2020-06-06 23:46:33
126
原创 【leetcode-python-9】1441. 用栈操作构建数组
【leetcode-python-9】1441. 用栈操作构建数组渣渣原始版(42.86%)参考大佬版(73.47%)参考大佬改进版(88.27%)leetcode 面试题09. 用两个栈实现队列渣渣原始版(42.86%)用了int转换,可能比较慢吧。class Solution(object): def buildArray(self, target, n): """ :type target: List[int] :type n: int
2020-06-06 22:57:33
191
原创 【leetcode-python-8】面试题59 - I. 滑动窗口的最大值
【leetcode-python-8】面试题59 - I. 滑动窗口的最大值参考大佬版1(77.12%)参考大佬版2(97.41%)参考大佬版3(89.50%)leetcode 面试题59 - I. 滑动窗口的最大值参考大佬版1(77.12%)这种类型的题得用单调栈。这个版本没有用deque。注:参考题解区Krahets思路。class Solution(object): def maxSlidingWindow(self, nums, k): """ :t
2020-06-06 21:56:25
287
原创 【leetcode-python-7】496. 下一个更大元素 I
【leetcode-python-7】496. 下一个更大元素 I超渣渣原始版(5.67%)渣渣原始版(59.18%)渣渣原始版(98.87%)leetcode 496. 下一个更大元素 I超渣渣原始版(5.67%)用了两个最烂的顺序遍历。python不用flag退出循环。class Solution(object): def nextGreaterElement(self, nums1, nums2): """ :type nums1: List[int]
2020-06-03 22:28:01
173
原创 【leetcode-python-6】155. 最小栈
【leetcode-python-6】155. 最小栈渣渣原始版(96.26%)参考官方版(70.46%)leetcode 155. 最小栈渣渣原始版(96.26%)一个栈实现普通栈的功能,另一个栈用来存最小值。有考虑过“哨兵”,算是进步。后面加了哨兵试了下,是90.63%。class MinStack(object): def __init__(self): """ initialize your data structure here.
2020-06-03 17:16:49
149
原创 【leetcode-python-5】682. 棒球比赛
【leetcode-python-5】682. 棒球比赛渣渣原始版(85.64%)参考官方版(68.09%)leetcode 682. 棒球比赛渣渣原始版(85.64%)读题读了很久才看懂(泪目),看来自己写的还可以哈hhhhhh,开心了。专门开个栈来存得分,数据多的时候,只用加一次嘛。class Solution(object): def calPoints(self, ops): """ :type ops: List[str] :rt
2020-06-02 21:56:52
180
原创 【leetcode-python-4】232. 用栈实现队列
【leetcode-python-4】232. 用栈实现队列渣渣原始版(86.03%)(对这个结果较为满意,没有尝试其他版)leetcode 232. 用栈实现队列渣渣原始版(86.03%)用的【leetcode-python-1】中参考大佬版的思路。class MyQueue(object): def __init__(self): """ Initialize your data structure here. """
2020-06-02 17:26:32
190
原创 【leetcode-python-3】1047. 删除字符串中的所有相邻重复项
【leetcode-python-3】1047. 删除字符串中的所有相邻重复项渣渣原始版(40.77%)参考官方版(60.09%)leetcode 1047. 删除字符串中的所有相邻重复项渣渣原始版(40.77%)用栈,不想pop只想查看栈顶数据的直接p[len§-1]哇。class Solution(object): def removeDuplicates(self, S): """ :type S: str :rtype: str
2020-06-02 16:47:37
296
1
原创 【leetcode-python-2】20. 有效的括号
【leetcode-python-2】20. 有效的括号渣渣原始版(67.71%)参考大佬版(84.42%)leetcode 20. 有效的括号渣渣原始版(67.71%)比较在q1,q2的下标。判断“是否为空”的语句有点多。class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ q1 = "([{"
2020-06-02 15:31:57
156
原创 【leetcode-python-1】面试题09. 用两个栈实现队列
【leetcode-python-1】面试题09. 用两个栈实现队列渣渣原始版(13%)参考大佬版(86%)leetcode 面试题09. 用两个栈实现队列渣渣原始版(13%)items2用于pop出head,做完以后复原,完整数据始终存在items1里。class CQueue1(object): def __init__(self): self.items1 = [] self.items2 = [] def appendTail(self,
2020-06-01 21:19:17
155
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人