
Leetcode
yeah_panda
这个作者很懒,什么都没留下…
展开
-
二分法Lintcode
875爱吃香蕉的珂珂class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: # 暴力 K_max = max(piles) for i in range(1, K_max + 1): if self.canEatAll(piles, i) <= H: return i #原创 2020-08-20 11:58:54 · 158 阅读 · 0 评论 -
二叉树,树的遍历
树的遍历深度优先搜索前序遍历class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: #递归解法 #result = [] #def preorder(root): # if not root: # return # result.append(root.val)原创 2020-08-19 13:57:10 · 141 阅读 · 0 评论 -
Leetcode 542 01矩阵
542 01矩阵class Solution: def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]: m, n = len(matrix), len(matrix[0]) dist = [[0] * n for _ in range(m)] zeroes_pos = [(i, j) for i in range(m) for j in range(n) if m原创 2020-08-16 20:35:11 · 99 阅读 · 0 评论 -
Leetcode 733图像渲染
733图像渲染class Solution: def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]: target = image[sr][sc] m, n = len(image), len(image[0]) q = collections.deque([(sr, sc)]) image[原创 2020-08-15 18:12:35 · 125 阅读 · 0 评论 -
Leetcode 394字符串解码
394字符串解码class Solution: def decodeString(self, s): stack = [] for string in s: if string != ']': stack.append(string) else: tmp = '' while stack[-1] != '[':原创 2020-08-15 18:11:12 · 143 阅读 · 0 评论 -
Leetcode 225用队列实现栈
225用队列实现栈class MyStack: def __init__(self): """ Initialize your data structure here. """ self.q1 = [] self.q2 = [] self.tp = None def push(self, x: int) -> None: """ Push element原创 2020-08-15 18:09:17 · 205 阅读 · 0 评论 -
Leetcode 232用栈实现队列
232用栈实现队列class MyQueue: def __init__(self): """ Initialize your data structure here. """ self.items = [] def push(self, x: int) -> None: """ Push element x to the back of queue. """原创 2020-08-15 18:07:49 · 138 阅读 · 0 评论 -
Leetcode 494目标和
494目标和class Solution: def findTargetSumWays(self, nums: List[int], S: int) -> int: d = {} def dfs(cur, i, d): if i < len(nums) and (cur, i) not in d: d[(cur, i)] = dfs(cur + nums[i], i+1, d) + dfs(cur原创 2020-08-15 18:06:00 · 171 阅读 · 0 评论 -
Leetcode 133克隆图
133克隆图"""# Definition for a Node.class Node: def __init__(self, val = 0, neighbors = []): self.val = val self.neighbors = neighbors"""class Solution: def cloneGraph(self, node: 'Node') -> 'Node': if not node:原创 2020-08-15 18:04:20 · 140 阅读 · 0 评论 -
Leetcode 150逆波兰表达式求值
150逆波兰表达式求值class Solution(object): def evalRPN(self, tokens): """ :type tokens: List[str] :rtype: int """ stack =[] for token in tokens: if token not in '+-*/': stack.append(t原创 2020-08-15 18:02:16 · 125 阅读 · 0 评论 -
Leetcode 739. 每日温度
739. 每日温度class Solution: def dailyTemperatures(self, T): stack = [] n = len(T) outputlist = [0] * n for i in range(n): while stack != [] and T[i] > T[stack[-1]]: top =stack.pop()原创 2020-08-15 18:00:41 · 97 阅读 · 0 评论 -
Leetcode 20有效的括号
20有效的括号class Solution: def isValid(self, s: str) -> bool: if not s: return True stack = collections.deque() for i in s: if i in '([{': stack.append(i) else:原创 2020-08-15 17:58:05 · 85 阅读 · 0 评论 -
Leetcode 155最小栈
155最小栈class MinStack(object): def __init__(self): """ initialize your data structure here. """ self.items = [] self.minitems = [] def push(self, x): """ :type x: int :rtype: None原创 2020-08-15 17:55:19 · 118 阅读 · 0 评论 -
Lintcode多重背包问题
多重背包问题#背包问题VIIclass Solution: """ @param n: the money of you @param prices: the price of rice[i] @param weight: the weight of rice[i] @param amounts: the amount of rice[i] @return: the maximum weight """ def backPackVII(se原创 2020-08-05 20:15:25 · 220 阅读 · 0 评论 -
Lintcode完全背包问题
完全背包问题完全背包:枚举每件物品取0,1,2,3 … m / A[i] 件,dp[i][j] = max(dp[i - 1][j],dp[i - 1][j - k * A[i]] + k * V[i])#背包问题IIIclass Solution: """ @param A: an integer array @param V: an integer array @param m: An integer @return: an array """原创 2020-08-05 14:36:58 · 149 阅读 · 0 评论 -
Lintcode 0-1背包问题
背包问题0-1背包问题状态转移方程dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]),i代表第几个物品,j代表背包容量,w[i - 1],v[i - 1]代表第i个物品的重量和价值。#背包问题Iclass Solution: """ @param m: An integer m denotes the size of a backpack @param A: Given n items with s原创 2020-08-05 00:01:46 · 392 阅读 · 0 评论 -
Leetcode279完全平方数
Leetcode279完全平方数class Solution: def numSquares(self, n: int) -> int: square_nums = [a * a for a in range(1, int(n**0.5+1))] level = 0 queue = [n] while queue: level += 1 next_que = set()原创 2020-08-04 12:10:39 · 175 阅读 · 0 评论 -
Leetcode752 打开转盘锁
Leetcode752 打开转盘锁class Solution: def openLock(self, deadends: List[str], target: str) -> int: def neighbor(node): y = list() for i in range(4): for j in (-1, 1): update = (int(nod原创 2020-08-04 12:08:59 · 133 阅读 · 0 评论 -
Leetcode200岛屿数量
Leetcode200岛屿数量bfsclass Solution: def numIslands(self, grid): if not grid or not grid[0]: return 0 self.m, self.n = len(grid), len(grid[0]) islands = 0 for row in range(self.m): for col in range原创 2020-08-04 12:07:30 · 145 阅读 · 0 评论 -
Leetcode286墙与门
286墙与门#286墙与门class Solution: def wallsAndGates(self, rooms): EMPTYROOM = 2**31 - 1 door = 0 neighbors = [[-1,0], [1,0], [0,-1], [0,1]] if not rooms: return row = len(rooms) col = len(rooms[0原创 2020-08-04 12:03:46 · 204 阅读 · 0 评论 -
Leetcode346数据流中的移动平均值
346数据流中的移动平均值#346数据流中的移动平均值from collections import dequefrom pythonds import Dequeclass MovingAverage(): def __init__(self, size): self.queue = Deque() self.maxsize = size self.count = 0 def nxt(self, val): se原创 2020-08-04 12:02:12 · 149 阅读 · 0 评论 -
Leetcode设计循环队列
设计循环队列class MyCircularQueue: def __init__(self, k: int): """ Initialize your data structure here. Set the size of the queue to be k. """ self.len = k self.values = [-1] * self.len self.front = 0原创 2020-08-04 12:00:34 · 190 阅读 · 0 评论 -
力扣:删除排序数组中的重复项
80. 删除排序数组中的重复项 IIclass Solution: def removeDuplicates(self, nums: List[int]) -> int: # 双指针O(N) length = len(nums) slow, fast = 1, 1 count = 1 while fast < length: if nums[fast] == nums[fast -原创 2020-07-22 22:17:08 · 186 阅读 · 0 评论 -
力扣Leetcode 118/119杨辉三角I/II
118.杨辉三角Iclass Solution: def generate(self, numRows: int) -> List[List[int]]: # 动态规划 O(N^2) triangle = [] for row_num in range(numRows): row = [None for _ in range(row_num + 1)] row[0], row[-1] = 1,原创 2020-07-22 15:43:02 · 100 阅读 · 0 评论 -
力扣Leetcode 153/154
hh原创 2020-07-22 11:11:16 · 199 阅读 · 0 评论