题目:
Description:Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).
Examples:
solution(‘abc’, ‘bc’) // returns true
solution(‘abc’, ‘d’) // returns false
自己解决方法:主用List中的contains来检查值是否存在;
public static boolean solution(String str,String ending) {
Listlist=new ArrayList();
char[] charStr=str.toCharArray();
char[] charEnding=ending.toCharArray();
if(charStr.length<charEnding.length) {
return false;
}
for(int i=0;i<charStr.length;i++) {
list.add(charStr[i]+"");
}
boolean isOk=true;
for(int j=0;j<charEnding.length;j++) {
System.out.println(“e:”+charEnding[j]);
isOk= list.contains(charEnding[j]+"");
}
return isOk;
}
测试:
System.out.println(solution(“abc”,“a”));
输出:true
System.out.println(solution(“abc”,“b”));
输出:true
System.out.println(solution(“abc”,“c”));
输出:true
System.out.println(solution(“abc”,“ab”));
输出:true
System.out.println(solution(“abc”,“ac”));
输出:true
System.out.println(solution(“abc”,“bc”));
输出:true
System.out.println(solution(“abc”,“abc”));
输出:true
System.out.println(solution(“abc”,“d”));
输出:false
有好的更好的方法请回复。