您的位置:首页 > 其它

LeetCode: Valid Palindrome 解题报告

2014-12-30 20:16 405 查看

Valid Palindrome

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.

/*
SOLUTION 2: Iterator2.
*/
public boolean isPalindrome(String s) {
if (s == null) {
return false;
}

int len = s.length();

boolean ret = true;

int left = 0;
int right = len - 1;

String sNew = s.toLowerCase();

while (left < right) {
// bug 1: forget a )
if (!Character.isLetterOrDigit(sNew.charAt(left))) {
left++;
// bug 2: Line 67: error: cannot find symbol: method isLetterOrDigital(char)
} else if (!Character.isLetterOrDigit(sNew.charAt(right))) {
right--;
} else if (sNew.charAt(left) != sNew.charAt(right)) {
return false;
} else {
left++;
right--;
}
}

return true;
}


View Code

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/IsPalindrome_2014_1229.java
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: