JAVA中返回值为字母时_LeetCode#524通过删除字母匹配到字典里最长单词-java中CompareTo方法用法以及Comparator中Compare方法返回值...
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/*
524. 通過刪除字母匹配到字典里最長(zhǎng)單詞
給定一個(gè)字符串和一個(gè)字符串字典,找到字典里面最長(zhǎng)的字符串,該字符串可以通過刪除給定字符串的某些字符來得到。如果答案不止一個(gè),返回長(zhǎng)度最長(zhǎng)且字典順序最小的字符串。如果答案不存在,則返回空字符串。
示例 1:
輸入:
s = "abpcplea", d = ["ale","apple","monkey","plea"]
輸出:
"apple"
示例 2:
輸入:
s = "abpcplea", d = ["a","b","c"]
輸出:
"a"
說明:
所有輸入的字符串只包含小寫字母。
字典的大小不會(huì)超過 1000。
所有輸入的字符串長(zhǎng)度不會(huì)超過 1000
*/
public class p524 {
/*public String findLongestWord(String s, List d) {
Collections.sort(d, new Comparator() {
@Override
public int compare(String o1, String o2) {
if(o1.length()!=o2.length()){
return o2.length()-o1.length();
}
else {
int len=o1.length();
return o1.compareTo(o2);
}
}
});
String res="";
for(String str:d){
if(isChildXulie(s,str))return str;
}
return res;
}*/
public String findLongestWord(String s, List d){
String res="";
for(String str:d){
if(isChildXulie(s,str)){
int len=str.length();
if(len>res.length()){
res=str;
}
else if(len==res.length()) {
res=res.compareTo(str)>0?str:res;
}
}
}
return res;
}
public boolean isChildXulie(String s1,String s2){//判斷S2是不是s1的子序列
int j=0;
for(int i=0;i
if(j
j++;
}
}
if (j==s2.length()){
return true;
}
return false;
}
}
運(yùn)行結(jié)果:
注釋掉的:
第二種方法:
CompareTo方法:
Comparator中Compare方法返回值:
返回值為1則交換兩個(gè)值,返回值小于等于0不做交換
原文:https://www.cnblogs.com/jifeng0902/p/13295365.html
總結(jié)
以上是生活随笔為你收集整理的JAVA中返回值为字母时_LeetCode#524通过删除字母匹配到字典里最长单词-java中CompareTo方法用法以及Comparator中Compare方法返回值...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高合汽车高管吐槽集度汽车:发布会“动画片
- 下一篇: visual studio 没有属性页_