最长特殊序列 II
class Solution {
boolean containsSub(String s,String p){
int i,j;
for(i=0,j=0;i<p.length()&&j<s.length();j++)
if(s.charAt(j)==p.charAt(i))
i++;
if(i>=p.length())return true;
return false;
}
void removeStr(String p,LinkedList<String>list){
if(list.contains(p)){
String s="";
for(int i=0;i<list.size();i++){
s=list.get(i);
if(s.length()<p.length()&&containsSub(p,s)){
removeStr(s,list);
}
}
list.remove(p);
}
}
public int findLUSlength(String[] strs) {
int n=strs.length;
HashMap<String,Integer>map=new HashMap<>();
for(String s:strs)
if(map.containsKey(s))
map.replace(s, map.get(s)+1);
else
map.put(s, 1);
LinkedList<String>list=new LinkedList<>();
for(String s:map.keySet())
list.addFirst(s);
list.sort(new Comparator<String>(){ @Override
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
return o2.length()-o1.length();
} });
int last=list.size(),i=0;
while(i<list.size()){
if(map.get(list.get(i))>1)
removeStr(list.get(i),list);
else
i++;
if(last==list.size())break;
else last=list.size();
}
if(list.size()==0)return -1;
return list.getFirst().length();
}
}
坑:判断包含关系和递归删除
最长特殊序列 II的更多相关文章
- Leetcode 522.最长特殊序列II
最长特殊序列II 给定字符串列表,你需要从它们中找出最长的特殊序列.最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列). 子序列可以通过删去字符串中的某些字符实现,但 ...
- Java实现 LeetCode 522 最长特殊序列 II(查找最长的非子序列的长度)
522. 最长特殊序列 II 给定字符串列表,你需要从它们中找出最长的特殊序列.最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列). 子序列可以通过删去字符串中的某些 ...
- [Swift]LeetCode522. 最长特殊序列 II | Longest Uncommon Subsequence II
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...
- 522 Longest Uncommon Subsequence II 最长特殊序列 II
详见:https://leetcode.com/problems/longest-uncommon-subsequence-ii/description/ C++: 方法一: class Soluti ...
- [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- 九度oj题目1342:寻找最长合法括号序列II
题目1342:寻找最长合法括号序列II(25分) 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:886 解决:361 题目描述: 假如给你一个由’(‘和’)’组成的一个随机的括号序列,当然 ...
- [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- P1091 合唱队形 DP 最长升序列维护
题目描述 NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,…,K1,2,…,K,他 ...
随机推荐
- ACM-寻宝
题目描述:寻宝 有这么一块神奇的矩形土地,为什么神奇呢?因为上面藏有很多的宝藏.该土地由N*M个小正方形土地格子组成,每个小正方形土地格子上,如果标有“E”,则表示该格可以通过:如果标有“X”,则表示 ...
- NO15 第一关课后考试
第一关课后考试: 1.创建目录/data/oldboy,并且在该目录下创建文件oldboy.txt,然后在文件oldboy.txt里写如内容:inet addr:10.0.0.8 Bcast:10.0 ...
- docker 构建php-fpm IMages(dockerfile)
好久没写blog 做什么? 复习nginx zabbix docker-compos mariadb 学习 jenkins ansible ELK k8s (kubeadm) 好了也 ...
- codeforces 586B:Laurenty and Shop
B. Laurenty and Shop time limit per test 1 second memory limit per test 256 megabytes input standard ...
- setup factory 设置自启动
Registry.SetValue(HKEY_LOCAL_MACHINE, "Software//Microsoft//Windows//CurrentVersion//Run", ...
- mysql IF-IFNULL和IF-ISNULL同样逻辑的运行差别
首先,目标记录是存在的 SELECT * FROM d_device_user_bind dub WHERE dub.`uid`='222222222221' 其次, SELECT dub.uid,d ...
- [LeetCode] 930. Binary Subarrays With Sum 二元子数组之和
In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0, ...
- UVA 10564 计数DP
也是经典的计数DP题,想练练手,故意不写记忆化搜索,改成递推,还是成功了嘞...不过很遗憾一开始WA了,原来是因为判断结束条件写个 n或s为0,应该要一起为0的,搞的我以为自己递推写挫了,又改了一下, ...
- javascript设计模式(1)——面向对象基础
用对象收编变量2种方式 1 函数式 var Object = { name:function(){ return this; }, email:function(){ return this; } } ...
- cf 758D - Ability To Convert
从后往前贪心就好了.各种各样0的情况太BT了.. (各种爆long long,f**k) #include<bits/stdc++.h> #define LL long long #def ...