标签:determine example question problem purpose
Total Accepted: 50466 Total Submissions: 227908My Submissions
Question Solution
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 class Solution {
public boolean isPalindrome(String s) {
int l=s.length();
if(l<=1)
return true;
int i=0;
int j=l-1;
while(i<j)
{
if((s.charAt(i)>=‘a‘&&s.charAt(i)<=‘z‘)||(s.charAt(i)>=‘A‘&&s.charAt(i)<=‘Z‘)||(s.charAt(i)>=‘0‘&&s.charAt(i)<=‘9‘))
{
if((s.charAt(j)>=‘a‘&&s.charAt(j)<=‘z‘)||(s.charAt(j)>=‘A‘&&s.charAt(j)<=‘Z‘)||(s.charAt(j)>=‘0‘&&s.charAt(j)<=‘9‘))
{
if(s.charAt(i)==s.charAt(j)||s.charAt(i)+32==s.charAt(j)||s.charAt(i)-32==s.charAt(j))
{
i++;
j--;
}
else
return false;
}
else
{
j--;
}
}
else
{
i++;
if((s.charAt(j)>=‘a‘&&s.charAt(j)<=‘z‘)||(s.charAt(j)>=‘A‘&&s.charAt(j)<=‘Z‘)||(s.charAt(j)>=‘0‘&&s.charAt(j)<=‘9‘))
j=j;
else
j--;
}
}
return true;
}
}
标签:determine example question problem purpose
原文地址:http://7061299.blog.51cto.com/7051299/1642291