本文共 690 字,大约阅读时间需要 2 分钟。
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例 1:
输入: "A man, a plan, a canal: Panama"
输出: true 示例 2:输入: "race a car"
输出: falsepublic boolean isPalindrome(String s) {
if(s == null || s.length() <=0 || s.trim() == null || s.trim().length() <=0)
return true;
char[] chars = s.toCharArray();
List list = new ArrayList();
for(char c : chars){
if(Character.isLetterOrDigit(c)){
list.add(c);
}
}
if(list != null && list.size() > 0){
int length = list.size();
for(int i=0;i<length/2;i++){
char prex = (char)list.get(i);
char letter = (char)list.get(length -1-i);
if(Character.toUpperCase(prex) != Character.toUpperCase(letter))
return false;
}
return true;
}
return true;
}
转载地址:https://blog.csdn.net/luxuiary/article/details/105990707 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!