很简单的判断回文的题目,双指针,跳过不考虑的字符即可,代码如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool isPalindrome(string s) { | |
int len = s.size(), i = 0, j = len - 1; | |
while(true) | |
{ | |
while(i < len && !isalnum(s[i]))++i; | |
while(j >= 0 && !isalnum(s[j]))--j; | |
if(i >= j)break; | |
if(tolower(s[i]) != tolower(s[j]))return false; | |
++i; | |
--j; | |
} | |
return true; | |
} | |
}; |
No comments:
Post a Comment