[leetcode] 522. 最长特殊序列 II 暴力 + 双指针

简介: [leetcode] 522. 最长特殊序列 II 暴力 + 双指针

题目链接:传送门


暴力 + 双指针

题意有点绕,大体意思是:

给定一个字符串序列,问最长的不是其他字符串的子序列的串的长度是多大,如果没有则返回-1


比如题目中给定的"aba","cdc","eae"三个字符串,每个字符串都可以是最长的不是其他字符串的子序列

aba不是cdc的子序列,并且也不是eae的子序列

cdc不是aba的子序列,并且也不是eae的子序列

eae不是aba的子序列,并且也不是cdc的子序列

所以说:结果为3(aba、cdc、eae的长度均为3)


而对于"aaa","aaa","aa" 来讲第一个字符串aaa的子序列aa是第二个字符串aaa和第三个字符串aa的子序列,所以说不满足条件,其余的均不满足,所以说结果为-1


思路:

首先,暴力枚举每个字符串s[i]为子序列的情况,并且判断该字符串是否为其他字符串的子序列,如果满足条件,便记录max{s[i]}


java_code:

public class Solution {
    public static int findLUSlength(String[] strs) {
        int ans = -1;
        int len = strs.length;
        for (int i = 0; i < len; i++) {
            boolean flag = true;
            for (int j = 0; j < len; j++) {
                if (i == j) continue;
                if (isSubStr(strs[i], strs[j])) {
                    flag = false;
                    break;
                }
            }
            if (flag) ans = Math.max(ans, strs[i].length());
        }
        return ans;
    }
    public static boolean isSubStr(String a, String b) {
        int lena = a.length();
        int lenb = b.length();
        int pa = 0, pb = 0;
        while (pa < lena && pb < lenb) {
            if (a.charAt(pa) == b.charAt(pb)) pa++;
            pb++;
        }
        if (pa != lena) return false;
        else return true;
    }
    public static void main(String[] args) {
    }
}


目录
相关文章
|
2月前
|
存储 算法
《LeetCode》—— 摆动序列
《LeetCode》—— 摆动序列
|
2月前
|
算法
LeetCode刷题---21.合并两个有序链表(双指针)
LeetCode刷题---21.合并两个有序链表(双指针)
|
2月前
|
算法
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
|
2月前
|
存储 算法
LeetCode刷题---209. 长度最小的子数组(双指针-滑动窗口)
LeetCode刷题---209. 长度最小的子数组(双指针-滑动窗口)
|
2月前
|
存储 容器
LeetCode刷题---11. 盛最多水的容器(双指针-对撞指针)
LeetCode刷题---11. 盛最多水的容器(双指针-对撞指针)
|
2月前
|
算法
LeetCode刷题---167. 两数之和 II - 输入有序数组(双指针-对撞指针)
LeetCode刷题---167. 两数之和 II - 输入有序数组(双指针-对撞指针)
|
2月前
|
存储 算法
LeetCode刷题--- 61. 旋转链表(快慢指针+闭合为环)
LeetCode刷题--- 61. 旋转链表(快慢指针+闭合为环)
|
2月前
|
算法 索引
LeetCode刷题--- 138. 复制带随机指针的链表(哈希表+迭代)
LeetCode刷题--- 138. 复制带随机指针的链表(哈希表+迭代)
|
2月前
|
存储 算法 索引
LeetCode刷题---链表经典问题(双指针)
LeetCode刷题---链表经典问题(双指针)
|
2月前
|
算法
LeetCode刷题---160. 相交链表(双指针-对撞指针)
LeetCode刷题---160. 相交链表(双指针-对撞指针)

热门文章

最新文章