LeetCode 522 最长特殊序列II[枚举 双指针] HERODING的LeetCode之路

在这里插入图片描述

解题思路:
解题的关键在于特殊序列的长度除了-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;
    }

};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HERODING77

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值