当前位置:网站首页>LeetCode 522 最长特殊序列II[枚举 双指针] HERODING的LeetCode之路
LeetCode 522 最长特殊序列II[枚举 双指针] HERODING的LeetCode之路
2022-06-27 10:16:00 【HERODING23】

解题思路:
解题的关键在于特殊序列的长度除了-1,最小也是最短序列的长度,所以根本不需要考虑不同序列中子序列之间的匹配情况,这是因为最长序列(即序列本身)就是最特殊子序列。遍历所有的序列对,判断是否能匹配,不能匹配,更新最长序列长度,最后返回,代码如下:
class Solution {
public:
int findLUSlength(vector<string>& strs) {
int n = strs.size();
int ans = -1;
for(int i = 0; i < n; i ++) {
bool check = true;
for(int j = 0; j < n; j ++) {
if(i != j && judge(strs[i], strs[j])) {
check = false;
break;
}
}
if(check) {
ans = max(ans, static_cast<int>(strs[i].size()));
}
}
return ans;
}
bool judge(string s1, string s2) {
int n1 = s1.size(), n2 = s2.size();
// 长度相同比较是否相同
if(n1 == n2) return s1 == s2;
int index1 = 0, index2 = 0;
// 长度不同比较是否子串
while(index1 < n1 && index2 < n2) {
if(s1[index1] == s2[index2]) {
index1 ++;
}
index2 ++;
}
return index1 == n1;
}
};
边栏推荐
- 12 necessary tools for network engineers
- C any() and aii() methods
- [STM32] Hal library stm32cubemx tutorial 12 - IIC (read AT24C02)
- Introduction to the use of Arduino progmem static storage area
- 实验笔记之——CARMEN (.log .clf)文件转换为rosbag
- Easy to understand Laplace smoothing of naive Bayesian classification
- Oracle连接MySQL报错IM002
- Exception in Chinese character fuzzy query of MySQL database
- R language plot visualization: visualize the normalized histograms of multiple data sets, add density curve KDE to the histograms, set different histograms to use different bin sizes, and add edge whi
- lvi-sam 总结
猜你喜欢

When does the mobile phone video roll off?

Multi thread implementation rewrites run (), how to inject and use mapper file to operate database

Audiotrack and audiolinker

Quelles sont les fonctions de base nécessaires au développement d'applications de commerce électronique en direct? Quelles sont les perspectives d'avenir?

新旧两个界面对比

lvi-sam 总结
![[hcie-rs review mind map] - STP](/img/b5/b89e59fe7f23bf23feeadb991acba7.png)
[hcie-rs review mind map] - STP

学习笔记之——数据集的生成

audiotrack与audioflinger
![[200 opencv routines] 212 Draw a slanted rectangle](/img/cf/da8fff386d011c939946326c55671f.png)
[200 opencv routines] 212 Draw a slanted rectangle
随机推荐
【云享新鲜】社区周刊·Vol.68-华为云招募工业智能领域合作伙伴,强力扶持+商业变现
C language learning day_ 06
JS all network request modes
Openpyxl table reading instance
用户认证技术
Future & CompletionService
LVI Sam summary
手机影像内卷几时休?
感应电机直接转矩控制系统的设计与仿真(运动控制matlab/simulink)
浅析基于边缘计算的移动AR实现(中)
Memory compression for win10
[200 opencv routines] 211 Draw vertical rectangle
On anchors in object detection
测试同学怎么参与codereview
前馈-反馈控制系统设计(过程控制课程设计matlab/simulink)
leetcode:968. 监控二叉树【树状dp,维护每个节点子树的三个状态,非常难想权当学习,类比打家劫舍3】
Reorganize common shell scripts for operation and maintenance frontline work
Ubuntu手动安装MySQL
详解各种光学仪器成像原理
Win10快捷键整理