当前位置:网站首页>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
总结
子序列判断 + 排序贪心最大开始
题目理解:最长特殊序列 = 最长的不是其他字符串子序列的序列
数据量小,每个拿出来判断一下即可(从长往短是贪心)
边栏推荐
- 10 常见网站安全攻击手段及防御方法
- Understand neural network structure and optimization methods
- 巴基斯坦安全部队开展反恐行动 打死7名恐怖分子
- A classic interview question covering 4 hot topics
- dns备用服务器信息,dns服务器地址(dns首选和备用填多少)
- Freemarker
- Freemarker
- Es update values based on Index Names and index fields
- Rockermq message sending mode
- Hitek power supply maintenance X-ray machine high voltage generator maintenance xr150-603-02
猜你喜欢

Installation and usage of source insight tool

快速入门CherryPy(1)

Flow chart of Alipay wechat payment business

Imx8qxp DMA resources and usage (unfinished)
Shortcut key bug, reproducible (it seems that bug is the required function [funny.Gif])

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

Markem Imaje Marken IMAS printer maintenance 9450e printer maintenance

Object contains copy method?

Getting started with webrtc: 12 Rtendpoint and webrtcendpoint under kurento

最全H桥电机驱动模块L298N原理及应用
随机推荐
This application failed to start because it could not find or load the QT platform plugin
Installation and use of SVN version controller
How much memory does the data type occupy? LongVsObject
Advanced mathematics Chapter 7 differential equations
The markdown plug-in of the browser cannot display the picture
es 根据索引名称和索引字段更新值
R语言使用econocharts包创建微观经济或宏观经济图、demand函数可视化需求曲线(demand curve)、自定义配置demand函数的参数丰富可视化效果
Improving efficiency or increasing costs, how should developers understand pair programming?
This, constructor, static, and inter call must be understood!
QT运行显示 This application failed to start because it could not find or load the Qt platform plugin
[MySQL basic] general syntax 1
ucore lab3
Demand visual Engineer
Semi-supervised Learning入门学习——Π-Model、Temporal Ensembling、Mean Teacher简介
使用aspose-slides将ppt转pdf
[diffusion model]
快速入门CherryPy(1)
Enumeration? Constructor? Interview demo
Getting started with webrtc: 12 Rtendpoint and webrtcendpoint under kurento
12个网络工程师必备工具