随笔分类 - leetcode
摘要:题目描述: 方法一: class Solution: def maxProfit(self, k: int, prices: List[int]) -> int: if len(prices) <= 1: return 0 if (k < len(prices) // 2) : dp = [[-pr
阅读全文
摘要:题目描述: 方法一: class Solution: def maxProfit(self, prices: List[int]) -> int: dp_i1_0 = 0 dp_i1_1 = float('-inf') dp_i2_0 = 0 dp_i2_1 = float('-inf') for
阅读全文
摘要:题目描述: 方法一: class Solution: def maxProfit(self, prices: List[int]) -> int: profit = 0 for i in range(1,len(prices)): tem = prices[i] - prices[i-1] if t
阅读全文
摘要:题目描述; 方法一:O(n) O(1) class Solution: def maxProfit(self, prices: List[int]) -> int: minprices = float('inf') maxprofit = 0 for i in prices: if i<minpri
阅读全文
摘要:题目描述: 第一次提交:动态规划 class Solution: def minimumTotal(self, triangle: List[List[int]]) -> int: n = len(triangle) for i in range(n-2,-1,-1): for j in range
阅读全文
摘要:题目描述: 第一次提交: class Solution: def getRow(self, rowIndex: int) -> List[int]: k = rowIndex pre = [1] + [0] * k res = [1] + [0] * k for i in range(1,k+1):
阅读全文
摘要:题目描述: 输入:{"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":null,"right":null,"val":4},"next":null,"right":{"$id":"4","l
阅读全文
摘要:题目描述: 方法一:迭代 class Solution: def flatten(self, root: TreeNode) -> None: """ Do not return anything, modify root in-place instead. """ cur = root while
阅读全文
摘要:恢复内容开始 题目描述: 方法一:O(n) O(n) class Solution: def sortedListToBST(self, head: ListNode) -> TreeNode: nums = [] while head: nums.append(head.val) head = h
阅读全文
摘要:题目描述: 方法一:O(n) O(n) class Solution: def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode: assert len(inorder)==len(postorder) if
阅读全文
摘要:题目描述: 方法一: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class
阅读全文
摘要:题目描述: 方法一: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class
阅读全文
摘要:题目描述: 方法一:递归O(n) O(n) # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right =
阅读全文
摘要:题目描述: 方法一:递归 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None cla
阅读全文
摘要:题目描述: 方法一:动态规划 O(n^2) O(n) class Solution: def numTrees(self, n: int) -> int: dp = [0]*(n+1) dp[0],dp[1] = 1,1 for i in range(2,n+1): for j in range(1
阅读全文
摘要:题目描述: 方法一:递归 class Solution: def postorderTraversal(self, root: TreeNode) -> List[int]: res = [] def helper(root): if not root: return helper(root.lef
阅读全文
摘要:恢复内容开始 题目描述: 方法一:递归 class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: res = [] def helper(root): if not root: return res.appen
阅读全文
摘要:题目描述: 方法一:迭代 class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: res,stack = [],[] while True: while root: stack.append(root) roo
阅读全文
摘要:题目描述: 方法一:暴力法 class Solution: def restoreIpAddresses(self, s: str) -> List[str]: n = len(s) res = [] # 判读是否满足ip的条件 def helper(tmp): if not tmp or (tmp
阅读全文
摘要:题目描述: 方法一: class Solution: def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: dummy = ListNode(0) dummy.next = head pre = dummy for
阅读全文
浙公网安备 33010602011771号