题目: 题解:
双指针法使用首尾指针向中间移动,若遇到标点符号,我们需要使用isalnum()判断跳过即可;若遇到大写字母,我们需要使用tolower()转换为小写字符即可,这个函数对于数字和小写字母不做转换仅仅针对大写字母。
代码如下:
class Solution {
public:
bool isPalindrome(string s
) {
if(s
.empty())return true;
int i
=0,j
=s
.size()-1;
while(i
<j
)
{
while(i
<j
&&!isalnum(s
[i
]))i
++;
while(i
<j
&&!isalnum(s
[j
]))j
--;
if(tolower(s
[i
++])!=tolower(s
[j
--]))return false;
}
return true;
}
};
转载请注明原文地址: https://mac.8miu.com/read-515533.html