当前位置:网站首页>Leetcode 算法面试冲刺 热题 HOT 100 刷题(406 416 437 438 448)(六十九)
Leetcode 算法面试冲刺 热题 HOT 100 刷题(406 416 437 438 448)(六十九)
2022-08-09 15:32:00 【大叔爱学习.】
406. 根据身高重建队列



一句话理解:就是先把这些人按照个头,从大到小排列,然后我们再按照第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
416. 分割等和子集

437. 路径总和 III

知道要用回溯,但是具体写不出来,一个是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
需要二刷。
438. 找到字符串中所有字母异位词

写了一个常规方法,超时了:
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
448. 找到所有数组中消失的数字

简单题,写了个暴力法,超时了
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
边栏推荐
猜你喜欢

求n的阶乘的两种方法

超级火的夏日小空调

网络——TCP拥塞控制

投入C语言

给我一个机会,帮你快速上手三子棋

Anatomy of Storage Size, Value Range, and Output Format of Basic Data Types in C Language

Leading practice | How the world's largest wine app uses design sprint to innovate the vivino model

2022年8月9日:用C#生成.NET应用程序--使用 Visual Studio Code 调试器,以交互方式调试 .NET 应用(不会,失败)

智慧灯杆网关智慧交通应用

继承关系下构造方法的访问特点
随机推荐
Apple Developer Account Apply for D-U-N-S Number
【Web渗透】信息收集篇——Google搜索引擎(一)
【服务器数据恢复】SAN LUN映射出错导致文件系统数据丢失的数据恢复案例
Chapter 3: Use of GEE Data (3.1-3.3)
转行做程序员,从月薪5k到30k,45岁测试员道出了一路的心酸
uni-app覆盖组件样式h5生效,微信小程序不生效的问题
ESP8266-Arduino编程实例-MQ-4气体传感器驱动
在追梦的路上,唯独脚踏实地,才能梦想成真
【完美解决v-if导致echart不显示问题】
SQL抖音面试题:送你一个万能模板,要吗?(重点、每个用户每月连续登录的最大天数)
良匠-手把手教你写NFT抢购软(三)
Codeforces Round #806 (Div. 4)||沉淀)血洗五道口
No need to pay for the 688 Apple developer account, xcode13 packaged and exported ipa, and provided others for internal testing
机器学习强基计划1-2:图文详解线性回归与局部加权线性回归+房价预测实例
前言:关于作者吴秋生博士与此书简介
网络——流量控制&可靠传输&滑动窗口
MySQL进阶学习
C语言基本数据类型的存储大小、取值范围、输出格式的解剖
2022年深圳杯数学建模A题代码思路-- 破除“尖叫效应”与“回声室效应”,走出“信息茧房”
uniapp 项目搭建