当前位置:网站首页>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
总结
子序列判断 + 排序贪心最大开始
题目理解:最长特殊序列 = 最长的不是其他字符串子序列的序列
数据量小,每个拿出来判断一下即可(从长往短是贪心)
边栏推荐
- Summary of three basic interview questions
- 多個類的設計
- 内存泄露的最直接表现
- Enumeration? Constructor? Interview demo
- 基于STM32设计的蓝牙健康管理设备
- dns备用服务器信息,dns服务器地址(dns首选和备用填多少)
- Installation and usage of source insight tool
- Obsidian 一周使用心得(配置、主题和插件)
- Shortcut key bug, reproducible (it seems that bug is the required function [funny.Gif])
- 微信小程序学习之五种页面跳转方法.
猜你喜欢

枚举?构造器?面试Demo

Take you to play with the camera module

Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher

Apache POI的读写

Principle and application of the most complete H-bridge motor drive module L298N

视频文件太大?使用FFmpeg来无损压缩它

提高效率 Or 增加成本,开发人员应如何理解结对编程?

There is no doubt that this is an absolutely elaborate project

Semi-supervised Learning入门学习——Π-Model、Temporal Ensembling、Mean Teacher简介

Prometheus alarm process and related time parameter description
随机推荐
Vector:: data() pointer usage details
[vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy
Pakistani security forces killed 7 terrorists in anti-terrorism operation
Installation and usage of source insight tool
This application failed to start because it could not find or load the QT platform plugin
es 根据索引名称和索引字段更新值
Some considerations on operation / method overloading for thread to release lock resources
Improving efficiency or increasing costs, how should developers understand pair programming?
ucore lab5
ucore lab4
集合框架 泛型LinkedList TreeSet
Location and solution of network connection failure of primary online mobile terminal Report
Semi-supervised Learning入门学习——Π-Model、Temporal Ensembling、Mean Teacher简介
有关二叉树的一些练习题
Design of multiple classes
The markdown plug-in of the browser cannot display the picture
多个类的设计
【生动理解】深度学习中常用的各项评价指标含义TP、FP、TN、FN、IoU、Accuracy
视频文件太大?使用FFmpeg来无损压缩它
How do I get the STW (pause) time of a GC (garbage collector)?