一句话理解:就是先把这些人按照个头,从大到小排列,然后我们再按照第2个数进行index插入。原理就是,比如第3个人要插入队列时,前面已经排好的2个人,身高都大于等于他。他只插在index位置,就是前面有几个人比他高。
class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
res = []
people = sorted(people, key = lambda x: (-x[0], x[1]))
for p in people:
if len(res) <= p[1]:
res.append(p)
elif len(res) > p[1]:
res.insert(p[1], p)
return res
下面是我写的代码:
class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
# 根据第一个1元素的负数,当第一个元素负数相等,根据第二个元素正数排序
people.sort(key = lambda x : (-x[0], x[1]))
res = []
for p, ind in people:
if len(res) < ind:
res.append([p, ind])
else:
res.insert(ind, [p, ind])
return res
知道要用回溯,但是具体写不出来,一个是root不一定是定点,而是每一个点都可能是root。这里没想明白。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
ps = []
cnt = 0
def backtrack(root, ps, cnt):
if not root: return 0
if sum(ps) == targetSum:
cnt += 1
ps.pop()
return
ps.append(root.val)
backtrack(root.left, ps, cnt)
backtrack(root.right, ps, cnt)
backtrack(root, ps, cnt)
return cnt
class Solution:
def pathSum(self, root: TreeNode, targetSum: int) -> int:
def rootSum(root, targetSum):
if root is None:
return 0
ret = 0
if root.val == targetSum:
ret += 1
ret += rootSum(root.left, targetSum - root.val)
ret += rootSum(root.right, targetSum - root.val)
return ret
if root is None:
return 0
ret = rootSum(root, targetSum)
ret += self.pathSum(root.left, targetSum)
ret += self.pathSum(root.right, targetSum)
return ret
下面是我写的:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
def helper(root, targetSum):
if not root: return 0
res = 0
if root.val == targetSum:
res += 1
res += helper(root.left, targetSum - root.val)
res += helper(root.right, targetSum - root.val)
return res
if not root: return 0
res = helper(root, targetSum)
res += self.pathSum(root.left, targetSum)
res += self.pathSum(root.right, targetSum)
return res
需要二刷。
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
res = []
length = len(p)
for i in range(len(s) - length + 1):
tmp_s = s[i: i + length]
tmp = "".join(sorted(tmp_s))
if tmp == p:
res.append(i)
return res
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
len_s, len_p = len(s), len(p)
if len_s < len_p:
return []
s_cnt = [0] * 26
p_cnt = [0] * 26
for i in range(len_p):
s_cnt[ord(s[i]) - 97] += 1
p_cnt[ord(p[i]) - 97] += 1
res = []
if s_cnt == p_cnt:
res.append(0)
for i in range(len_s - len_p):
s_cnt[ord(s[i]) - 97] -= 1
s_cnt[ord(s[i + len_p]) - 97] += 1
if s_cnt == p_cnt:
res.append(i + 1)
return res
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
n = len(nums)
res = []
for i in range(1, n + 1):
if i not in nums:
res.append(i)
return res
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
n = len(nums)
for num in nums:
x = (num - 1) % n
nums[x] += n
ret = [i + 1 for i, num in enumerate(nums) if num <= n]
return ret
哈希的方法:
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
dic = {}
for num in nums:
if num in dic:
dic[num] += 1
else:
dic[num] = 0
res = []
for i in range(1, len(nums) + 1):
if i not in dic:
res.append(i)
return res