标签:ons 排名 正则表达式 数字 ack 调用 ringbuf nbsp not
2017/3/15 21:47:04
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.
Subscribe to see which companies asked this question.
publicclassSolution{
publicboolean isPalindrome(String s){
for(int i=0,j=s.length()-1;i<j;)
if(Character.isLetterOrDigit(s.charAt(i))&&Character.isLetterOrDigit(s.charAt(j)))
if(Character.toLowerCase(s.charAt(i))!=Character.toLowerCase(s.charAt(j)))returnfalse;
else{
i++;j--;
}
elseif(!Character.isLetterOrDigit(s.charAt(i))) i++;
elseif(!Character.isLetterOrDigit(s.charAt(j))) j--;
returntrue;
}
}
publicclassSolution{
publicboolean isPalindrome(String s){
for(int i=0,j=s.length()-1;i<j;)
if(Character.isLetterOrDigit(s.charAt(i))&&Character.isLetterOrDigit(s.charAt(j))){
if(s.charAt(i)== s.charAt(j)||(s.charAt(i)>64&& s.charAt(j)>64&&Math.abs(s.charAt(i)- s.charAt(j))==32)){i++;j--;continue;};
returnfalse;
}
elseif(!Character.isLetterOrDigit(s.charAt(i))) i++;
elseif(!Character.isLetterOrDigit(s.charAt(j))) j--;
returntrue;
}
}
publicclassSolution{
publicboolean isPalindrome(String s){
String actual = s.replaceAll("[^A-Za-z0-9]","").toLowerCase();
returnnewStringBuffer(actual).reverse().toString().equals(actual);
}
}
标签:ons 排名 正则表达式 数字 ack 调用 ringbuf nbsp not
原文地址:http://www.cnblogs.com/flyfatty/p/6624794.html