随笔分类 -  LeetCode

摘要:# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def isBalanced(self, root: TreeNode) -> bool: if r 阅读全文
posted @ 2019-10-09 21:21 我叫郑小白 阅读(120) 评论(0) 推荐(0)
摘要:class Solution: def sortColors(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ head, now, tail = 0, 0, len(nums)-1 while now <= tail: if nums[now] == 0: n 阅读全文
posted @ 2019-09-16 21:03 我叫郑小白 阅读(177) 评论(0) 推荐(0)
摘要:class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: """ Do not return anything, modify nums1 in-place instead. """ while m > 0 and n > 0: if nums1[m-1] <= nums 阅读全文
posted @ 2019-09-04 17:45 我叫郑小白 阅读(122) 评论(0) 推荐(0)
摘要:class Solution: def climbStairs(self, n: int) -> int: # 斐波拉且数列 prev, current = 0, 1 for i in range(n): prev, current = current, prev + current return current 阅读全文
posted @ 2019-09-03 19:43 我叫郑小白 阅读(107) 评论(0) 推荐(0)
摘要:# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: current = head 阅读全文
posted @ 2019-09-03 19:34 我叫郑小白 阅读(123) 评论(0) 推荐(0)
摘要:# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def isSymmetric(... 阅读全文
posted @ 2019-09-03 19:14 我叫郑小白 阅读(113) 评论(0) 推荐(0)
摘要:# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def minDepth(self, root: TreeNode) -> int: if root 阅读全文
posted @ 2019-09-03 18:57 我叫郑小白 阅读(136) 评论(0) 推荐(0)
摘要:# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def maxDepth(sel... 阅读全文
posted @ 2019-08-28 21:38 我叫郑小白 阅读(91) 评论(0) 推荐(0)
摘要:# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def levelOrderBottom(self, root: TreeNode) -> List[Li... 阅读全文
posted @ 2019-08-28 21:33 我叫郑小白 阅读(112) 评论(0) 推荐(0)
摘要:# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def sortedArrayT... 阅读全文
posted @ 2019-08-28 19:11 我叫郑小白 阅读(122) 评论(0) 推荐(0)
摘要:# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def isSameTree(self, p: TreeNode, q: TreeNode) -> 阅读全文
posted @ 2019-08-28 18:53 我叫郑小白 阅读(160) 评论(0) 推荐(0)
摘要:class Solution: def plusOne(self, digits: List[int]) -> List[int]: for i in reversed(range(len(digits))): if digits[i] == 9: digits[i] = 0 else: digits[i] += 1 return digits digits[0] = 1 digits.appen 阅读全文
posted @ 2019-08-26 22:26 我叫郑小白 阅读(103) 评论(0) 推荐(0)
摘要:class Solution: def mySqrt(self, x: int) -> int: if x x / mid: right = mid - 1 else: left = mid + 1 return left - 1 阅读全文
posted @ 2019-08-26 22:05 我叫郑小白 阅读(139) 评论(0) 推荐(0)
摘要:class Solution: def addBinary(self, a: str, b: str) -> str: result, carry, val = '', 0, 0 for i in range(max(len(a), len(b))): val = carry if i < len(a): val += int(a[-(i+1)]) if i < len(b): val += in 阅读全文
posted @ 2019-08-26 21:44 我叫郑小白 阅读(133) 评论(0) 推荐(0)
摘要:class Solution: def lengthOfLastWord(self, s: str) -> int: count = 0 local_count = 0 for i in range(len(s)): if s[i] == ' ': local_count = 0 else: local_count += 1 count = local_count return count 阅读全文
posted @ 2019-08-23 21:03 我叫郑小白 阅读(138) 评论(0) 推荐(0)
摘要:class Solution: def reverseWords(self, s: str) -> str: if s == '': return s ls = s.split() # " " if ls == []: return '' result = '' for i in range(0, len(ls)): result += ls[-1 - i] + ' ' # strip()去除首尾 阅读全文
posted @ 2019-08-23 21:02 我叫郑小白 阅读(203) 评论(0) 推荐(0)
摘要:class Solution: def maxSubArray(self, nums: List[int]) -> int: if max(nums) < 0: return nums local_max, global_max = 0, 0 for num in nums: local_max = max(0, local_max + num) global_max = max(global_m 阅读全文
posted @ 2019-08-23 18:46 我叫郑小白 阅读(109) 评论(0) 推荐(0)
摘要:class Solution: def countAndSay(self, n: int) -> str: seq = "1" for i in range(n-1): seq = self.getNext(seq) return seq def getNext(self, seq): i, next_seq = 0, '' while i < len(seq): count = 1 while 阅读全文
posted @ 2019-08-23 18:29 我叫郑小白 阅读(100) 评论(0) 推荐(0)
摘要:class Solution: def permute(self, nums: List[int]) -> List[List[int]]: if len(nums) <= 1: return [nums] answer = [] for i, num in enumerate(nums): n = nums[:i] + nums[i+1:] for y in self.permute(n): a 阅读全文
posted @ 2019-08-22 15:27 我叫郑小白 阅读(92) 评论(0) 推荐(0)
摘要:class Solution: def lenLongestFibSubseq(self, A: List[int]) -> int: s = set(A) n = len(A) result = 0 for i in range(n-1): for j in range(i+1, n): a, b = A[i], A[j] count = 2 while a+b in s: a, b = b, 阅读全文
posted @ 2019-08-22 11:38 我叫郑小白 阅读(222) 评论(0) 推荐(0)