随笔分类 - leetcode
摘要:题目描述: 方法一:正则 import re class Solution: def myAtoi(self, str: str) -> int: INT_MAX = 2149483647 INT_MIN = -2147483648 num = re.findall(r'^[\+\-]?\d+',s
阅读全文
摘要:题目描述: 方法一: class Solution: def convert(self, s: str, numRows: int) -> str: if not s: return "" if numRows == 1: return s s_Rows = [""] * numRows i = 0
阅读全文
摘要:方法一:动态规划 O(n2) O(n2) class Solution: def longestPalindrome(self, s: str) -> str: size = len(s) if size <= 1: return s # 二维 dp 问题 # 状态:dp[l,r]: s[l:r]
阅读全文
摘要:题目描述: 方法一:O(N) class Solution: def lengthOfLongestSubstring(self, s: str) -> int: slow = 0 fast = 0 res_max = 0 table = dict() while slow<len(s) and f
阅读全文
摘要:题目描述: 方法一: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def addTw
阅读全文
摘要:题目描述: 方法一: class Solution(object): def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ p = ListNode(0) # 创建新链表头 p.next = h
阅读全文
摘要:题目描述: 方法一;回溯 class Solution: def exist(self, board: List[List[str]], word: str) -> bool: max_x,max_y,max_step = len(board)-1,len(board[0])-1,len(word)
阅读全文
摘要:题目描述: 第一次提交;超时 class Solution: def jump(self, nums: List[int]) -> int: l = [] for i in range(len(nums)): l.append(i) for i, v in enumerate(nums): for
阅读全文
摘要:题目描述: 第一次提交: class Solution: def canJump(self, nums: List[int]) -> bool: if len(nums)<=1: return True for i,v in enumerate(nums): if i == 0 and v == 0
阅读全文
摘要:题目描述; 第一次提交; class Solution: def isUnivalTree(self, root: TreeNode) -> bool: if root == None: return True if root.left!=None and root.left.val!=root.v
阅读全文
摘要:题目描述: 方法一: class Solution: def findMode(self, root: TreeNode) -> List[int]: if not root: return [] dic = {} stack = [root] while stack: node = stack.p
阅读全文
摘要:题目描述: 方法一:栈 class Solution(object): def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: int """ count = 0 if root == None: r
阅读全文
摘要:题目描述: 方法一:递归 class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int: if not root: return 0 if root.left and root.left.left == None and root.
阅读全文
摘要:题目描述: 利用二叉搜索树的特点,如果p、q的值都小于root,说明p q 肯定在root的左子树中;如果p q都大于root,说明肯定在root的右子树中,如果一个在左一个在右 则说明此时的root记为对应的最近公共祖先 方法一:递归 class Solution: def lowestCommo
阅读全文
摘要:题目描述: 第一次提交:参考113-路径总和② class Solution: def binaryTreePaths(self, root: TreeNode) -> List[str]: r = [] if not root: return r l = "" def path(root, l):
阅读全文
摘要:题目描述: 第一次提交: class Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if not root: return None temp = root.le
阅读全文
摘要:题目描述: 参考后的提交: class Solution(object): def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: List[List[int]] """ r = [] l = []
阅读全文
摘要:题目描述: 第一次提交: class Solution(object): def hasPathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: bool """ if not root : return Fa
阅读全文
摘要:题目描述: 第一次提交: class Solution(object): def minDepth(self, root): """ :type root: TreeNode :rtype: int """ if not root: return 0 if root.left and root.ri
阅读全文
摘要:恢复内容开始 题目描述: 方法一: class Solution(object): def isBalanced(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True if abs(self.c
阅读全文
浙公网安备 33010602011771号