1 解题思想
就一个简单的判断一个string里面的字符和数字部分,是否是回文的,不考虑符号和大小写等
其实说白了,就首尾指针对比一下就好。。 我代码里面首尾不同的地方时过滤下非字符数字
2 原题
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
3 AC解
public class Solution {
/**
* 两个指针,注意只考虑数字和字母,而且不考虑大小写
* */
public boolean isPalindrome(String s) {
int start=0,end=s.length()-1;
s=s.toLowerCase();
while(start<end){
while(start<end && !(s.charAt(start)>='a' && s.charAt(start)<='z') && !(s.charAt(start)>='0' && s.charAt(start)<='9')) start++;
while(start<end && !(s.charAt(end)>='a' && s.charAt(end)<='z') && !(s.charAt(end)>='0' && s.charAt(end)<='9')) end--;
if(start>=end)
break;
if(s.charAt(start)!=s.charAt(end)) return false;
start++;
end--;
}
return true;
}
}
最近频繁发生因为leetcode题目测试数据变动而造成的代码不能AC,所以这里特别贴出AC
因为代码完成时间长短不定,所以如果我给的代码不能AC了,请及时评论通知我,我会及时修改的