当前位置:网站首页>leetcode:522. 最长特殊序列 II【贪心 + 子序列判断】
leetcode:522. 最长特殊序列 II【贪心 + 子序列判断】
2022-06-27 09:35:00 【白速龙王的回眸】

分析
从最长开始来判断(排序贪心)
如果当前的str,不是其余str的子序列,那么它就是最长的特殊序列
子序列的判断使用双指针追踪方法,看看小的序列能不能走完即可
ac code
class Solution:
def findLUSlength(self, strs: List[str]) -> int:
# a是否为b的子序列
# 双指针追踪(经典)
def isSubSequnce(a, b):
n, m = len(a), len(b)
i = j = 0
while i < n and j < m:
if a[i] == b[j]:
i += 1
j += 1
return i == n
n = len(strs)
strs.sort(key = lambda x: -len(x))
#res = -1
for i in range(n):
flag = True
nstrs = strs[:i] + strs[i + 1:]
# 当前的不能是其他任何字符串的子序列!
for ss in nstrs:
if isSubSequnce(strs[i], ss):
flag = False
break
if flag:
return len(strs[i])
return -1
总结
子序列判断 + 排序贪心最大开始
题目理解:最长特殊序列 = 最长的不是其他字符串子序列的序列
数据量小,每个拿出来判断一下即可(从长往短是贪心)
边栏推荐
- A classic interview question covering 4 hot topics
- 【OpenCV 例程200篇】211. 绘制垂直矩形
- JS 文件上传下载
- prometheus告警流程及相关时间参数说明
- 使用Aspose.cells将Excel转成PDF
- Process 0, process 1, process 2
- 如何获取GC(垃圾回收器)的STW(暂停)时间?
- 谷歌浏览器 chropath插件
- Only one ConfirmCallback is supported by each RabbitTemplate 解决办法
- Pakistani security forces killed 7 terrorists in anti-terrorism operation
猜你喜欢

谷歌浏览器 chropath插件

Win10 add right-click menu for any file

IO pin configuration and pinctrl drive

I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!

Hitek power supply maintenance X-ray machine high voltage generator maintenance xr150-603-02

I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!

Object contains copy method?

Google browser chropath plug-in

不容置疑,这是一个绝对精心制作的项目

别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
随机推荐
IO pin configuration and pinctrl drive
快速入门CherryPy(1)
Demand visual Engineer
QT运行显示 This application failed to start because it could not find or load the Qt platform plugin
unity--newtonsoft.json解析
巴基斯坦安全部队开展反恐行动 打死7名恐怖分子
12个网络工程师必备工具
NoSQL database redis installation
i=i++;
Improving efficiency or increasing costs, how should developers understand pair programming?
队列,双向队列,及其运用
初步认识pytorch
【报名】基础架构设计:从架构热点问题到行业变迁 | TF63
Principle and application of the most complete H-bridge motor drive module L298N
Obsidian 一周使用心得(配置、主题和插件)
分布式文件存储系统的优点和缺点
运维一线工作常用shell脚本再整理
TDengine 邀请函:做用技术改变世界的超级英雄,成为 TD Hero
Freemarker
openpyxl表格读取实例