当前位置:网站首页>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
总结
子序列判断 + 排序贪心最大开始
题目理解:最长特殊序列 = 最长的不是其他字符串子序列的序列
数据量小,每个拿出来判断一下即可(从长往短是贪心)
边栏推荐
- E+h secondary meter repair pH transmitter secondary display repair cpm253-mr0005
- Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher
- 不容置疑,这是一个绝对精心制作的项目
- fastadmin 安装后访问后台提示模块不存在
- 队列,双向队列,及其运用
- 最全H桥电机驱动模块L298N原理及应用
- 1098 insertion or heap sort (PAT class a)
- IO pin configuration and pinctrl drive
- ucore lab4
- The background prompt module for accessing fastadmin after installation does not exist
猜你喜欢

How do I get the STW (pause) time of a GC (garbage collector)?
![[system design] proximity service](/img/02/57f9ded0435a73f86dce6eb8c16382.png)
[system design] proximity service
Shortcut key bug, reproducible (it seems that bug is the required function [funny.Gif])

IO pin configuration and pinctrl drive

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

One week's experience of using Obsidian (configuration, theme and plug-in)
![[vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy](/img/d6/119f32f73d25ddd97801f536d68752.png)
[vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy

Process 0, process 1, process 2

别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!

隐私计算FATE-离线预测
随机推荐
Shortcut key bug, reproducible (it seems that bug is the required function [funny.Gif])
Installation and usage of source insight tool
JS 客户端存储
Tips for using Jupiter notebook
ucore lab3
js中的数组对象
main()的参数argc与argv
借助原子变量,使用CAS完成并发操作
有關二叉樹的一些練習題
看看volatile你深知多少
【生动理解】深度学习中常用的各项评价指标含义TP、FP、TN、FN、IoU、Accuracy
多個類的設計
巴基斯坦安全部队开展反恐行动 打死7名恐怖分子
win10为任意文件添加右键菜单
Es update values based on Index Names and index fields
ThreadLocal digs its knowledge points again
Reading and writing Apache poi
std::memory_ order_ seq_ CST memory order
Freemarker
Use aspese Cells convert Excel to PDF