摘要:
注意第K大,要用小顶堆。因为get取走的都是小的,留下的k个都是最大的。 from queue import PriorityQueue class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: pq = Pr 阅读全文
摘要:
python3 先对左边进行backtrace,然后根据奇偶添加右边的。 class Solution: def findStrobogrammatic(self, n: int) -> List[str]: if n == 0: return [] charDict = {'0': '0', '1 阅读全文
摘要:
双指针 class Solution: def moveZeroes(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ i = j = 0 while j < l 阅读全文
摘要:
反转0,会将前一段和这一段拼起来。所以记录上一段1的个数和这一段1的个数。 class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: maxCnt = 0 lastCnt = -1 thisCnt = 0 fo 阅读全文
摘要:
非常简单,注意边界 class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: maxCnt = 0 cnt = 0 for i in range(len(nums)): if nums[i] == 1: cnt 阅读全文
摘要:
python3 要原地变换,就要利用int的位数。这里用了第二位,就是%2以及+2的做法,不改变奇偶性。 class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, 阅读全文
摘要:
python3 小心结尾。另外,zzz为s,zz为substr时,整个字符串都满足,所以要一个一个移动。 答案里用了startswith,更直观 class Solution: def addBoldTag(self, s: str, dict: List[str]) -> str: mark = 阅读全文
摘要:
Python3,双指针,注意K为0的情况。 class Solution: def lengthOfLongestSubstringKDistinct(self, s: str, k: int) -> int: if k == 0: return 0 charMap = {} result = 0 阅读全文
摘要:
注意python的字符和数字转化函数是chr和ord class Solution: def licenseKeyFormatting(self, S: str, K: int) -> str: lst = [] for c in S: if c == '-': continue if c >= ' 阅读全文
摘要:
要注意边和节点数是不一样的 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None cl 阅读全文
摘要:
其实暴力也能解,因为就4*4*4*4种 class Solution: def findNext(self, char, nums): i = 0 while i < len(nums): if nums[i] == char: if i != len(nums) - 1: return nums[ 阅读全文