0%

valid-palindrome

Valid Palindrome – LeetCode 125

Problem

Description

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Answer

Original

Code

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
bool isPalindrome(string s) {
for (int i = 0, j = s.size() - 1; i < j; i++, j--) {
while (isalnum(s[i]) == false && i < j) i++;
while (isalnum(s[j]) == false && i < j) j--;
if (toupper(s[i]) != toupper(s[j])) return false;
}
return true;
}
};

思路

简单的头尾同时遍历比对,时间复杂度$O(n)$,空间复杂度$O(1)$。

Better

思路

没看到更好的