每天一题;
为了明天的六级,今晚就早点做了;还好做的是回文,看起来简单但是写好不容易,有点小粗心,多交了好多次~
1、认真读题,不能认为自己做过就粗心大意;
2、要有时间学学正则表达式;
3、用现成的方法不一定是好事儿,效率问题;
当我第一次写的时候,直接用String的方法替换字符串,然后转换大小写,然后去除空格,这样自己代码量下来了,时间上去了,然后就自己过滤了一遍,自己处理这些逻辑,时间就降下来了,用事实来说话~
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.
public boolean isPalindrome(String s) {
int i = 0;
int j = s.length() - 1;
char a,b;
while (i < j) {
if (s.charAt(i) < 48 || (s.charAt(i) > 57 && s.charAt(i) < 65) ||
(s.charAt(i) > 90 && s.charAt(i) < 97) || s.charAt(i) > 122){
i++;
continue;
}
if (s.charAt(j) < 48 || (s.charAt(j) > 57 && s.charAt(j) < 65) ||
(s.charAt(j) > 90 && s.charAt(j) < 97) || s.charAt(j) > 122){
j--;
continue;
}
a = s.charAt(i);
b = s.charAt(j);
if(s.charAt(i)>96 &&s.charAt(i)<123 ){
a = (char)(s.charAt(i) - 32);
}
if(s.charAt(j)>96 &&s.charAt(j)<123 ){
b = (char)(s.charAt(j) - 32);
}
if (a == b) {
i++;
j--;
} else {
break;
}
}
if (i == j || (i - j == 2) || i - j == 1)
return true;
else
return false;
}