随笔分类 -  leetcode

摘要:题目描述: 方法一:正则 import re class Solution: def myAtoi(self, str: str) -> int: INT_MAX = 2149483647 INT_MIN = -2147483648 num = re.findall(r'^[\+\-]?\d+',s 阅读全文
posted @ 2019-07-09 11:21 oldby 阅读(249) 评论(0) 推荐(0)
摘要:题目描述: 方法一: class Solution: def convert(self, s: str, numRows: int) -> str: if not s: return "" if numRows == 1: return s s_Rows = [""] * numRows i = 0 阅读全文
posted @ 2019-07-08 20:46 oldby 阅读(219) 评论(0) 推荐(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] 阅读全文
posted @ 2019-07-08 19:52 oldby 阅读(315) 评论(0) 推荐(0)
摘要:题目描述: 方法一: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 阅读全文
posted @ 2019-07-08 17:44 oldby 阅读(328) 评论(0) 推荐(0)
摘要:题目描述: 方法一: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def addTw 阅读全文
posted @ 2019-07-08 16:50 oldby 阅读(176) 评论(0) 推荐(0)
摘要:题目描述: 方法一: class Solution(object): def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ p = ListNode(0) # 创建新链表头 p.next = h 阅读全文
posted @ 2019-05-07 18:16 oldby 阅读(171) 评论(0) 推荐(0)
摘要:题目描述: 方法一;回溯 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) 阅读全文
posted @ 2019-04-22 19:33 oldby 阅读(166) 评论(0) 推荐(0)
摘要:题目描述: 第一次提交;超时 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 阅读全文
posted @ 2019-04-19 19:54 oldby 阅读(423) 评论(0) 推荐(0)
摘要:题目描述: 第一次提交: 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 阅读全文
posted @ 2019-04-19 17:19 oldby 阅读(196) 评论(0) 推荐(0)
摘要:题目描述; 第一次提交; class Solution: def isUnivalTree(self, root: TreeNode) -> bool: if root == None: return True if root.left!=None and root.left.val!=root.v 阅读全文
posted @ 2019-03-31 18:28 oldby 阅读(123) 评论(0) 推荐(0)
摘要:题目描述: 方法一: class Solution: def findMode(self, root: TreeNode) -> List[int]: if not root: return [] dic = {} stack = [root] while stack: node = stack.p 阅读全文
posted @ 2019-03-30 20:53 oldby 阅读(256) 评论(0) 推荐(0)
摘要:题目描述: 方法一:栈 class Solution(object): def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: int """ count = 0 if root == None: r 阅读全文
posted @ 2019-03-29 20:12 oldby 阅读(125) 评论(0) 推荐(0)
摘要:题目描述: 方法一:递归 class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int: if not root: return 0 if root.left and root.left.left == None and root. 阅读全文
posted @ 2019-03-29 19:20 oldby 阅读(110) 评论(0) 推荐(0)
摘要:题目描述: 利用二叉搜索树的特点,如果p、q的值都小于root,说明p q 肯定在root的左子树中;如果p q都大于root,说明肯定在root的右子树中,如果一个在左一个在右 则说明此时的root记为对应的最近公共祖先 方法一:递归 class Solution: def lowestCommo 阅读全文
posted @ 2019-03-29 18:15 oldby 阅读(172) 评论(0) 推荐(0)
摘要:题目描述: 第一次提交:参考113-路径总和② class Solution: def binaryTreePaths(self, root: TreeNode) -> List[str]: r = [] if not root: return r l = "" def path(root, l): 阅读全文
posted @ 2019-03-28 18:05 oldby 阅读(148) 评论(0) 推荐(0)
摘要:题目描述: 第一次提交: class Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if not root: return None temp = root.le 阅读全文
posted @ 2019-03-27 22:11 oldby 阅读(117) 评论(0) 推荐(0)
摘要:题目描述: 参考后的提交: class Solution(object): def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: List[List[int]] """ r = [] l = [] 阅读全文
posted @ 2019-03-27 19:18 oldby 阅读(92) 评论(0) 推荐(0)
摘要:题目描述: 第一次提交: class Solution(object): def hasPathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: bool """ if not root : return Fa 阅读全文
posted @ 2019-03-27 15:03 oldby 阅读(138) 评论(0) 推荐(0)
摘要:题目描述: 第一次提交: class Solution(object): def minDepth(self, root): """ :type root: TreeNode :rtype: int """ if not root: return 0 if root.left and root.ri 阅读全文
posted @ 2019-03-27 12:10 oldby 阅读(140) 评论(0) 推荐(0)
摘要:恢复内容开始 题目描述: 方法一: class Solution(object): def isBalanced(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True if abs(self.c 阅读全文
posted @ 2019-03-26 22:04 oldby 阅读(190) 评论(0) 推荐(0)