Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama"
is a palindrome.
"race a car"
is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
题意:判断字符串是不是有效回文串,忽略掉标点,只考虑字母和数字,字母忽略大小写。
思路:水题,上代码
java代码:
public boolean isPalindrome(String s) {
s = s.toLowerCase();
char[] ss = s.toCharArray();
List<Character> list = new ArrayList<>();
for (int i = 0; i < ss.length; i++) {
if ((ss[i] >= 'a' && ss[i] <= 'z') || (ss[i] <= 'Z' && ss[i] >= 'A') || (ss[i] <= '9') && ss[i] >= '0') {
list.add(ss[i]);
}
}
Character[] sss = new Character[list.size()];
list.toArray(sss);
for (int i = 0; i < sss.length / 2; i++) {
if (sss[i] != sss[sss.length - i - 1]) {
return false;
}
}
return true;
}
网友评论