LeetCode 125: Valid Palindrome
程序员文章站
2024-03-06 21:56:14
...
问题描述
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: “A man, a plan, a canal: Panama”
Output: true
Example 2:
Input: “race a car”
Output: false
实现
class Solution {
public boolean isPalindrome(String s) {
String ss = s.replaceAll("[^0-9a-zA-Z]", "").toLowerCase();
if (s.length() == 0 || ss.length()<=1 )
return true;
int ps = 0;
int pe = ss.length() - 1;
boolean result = true;
while (ps <= pe) {
if (ss.charAt(ps++) != ss.charAt(pe--)) {
result = false;
break;
}
}
return result;
}
}
推荐阅读
-
[String][3]Leetcode125. Valid Palindrome
-
LeetCode125. Valid Palindrome
-
LeetCode 125: Valid Palindrome
-
Valid Palindrome II
-
Valid Palindrome II
-
leetcode 125 Valid Palindrome
-
LeetCode125. Valid Palindrome
-
[leetcode] Longest Valid Parentheses
-
LeetCode 193. Valid Phone Numbers
-
LeetCode Palindrome Partitioning