#include
#include
using namespace std;
bool isZimu(char ch)
{
if (ch >= ‘a’ && ch <= ‘z’ || ch >= ‘A’ && ch <= ‘Z’)
{
return true;
}
else
{
return false;
}
}
bool ValidPalindrome(string str)
{
int left = 0;
int right = str.size() - 1;
while (left < right)
{
while(left<right && !isZimu(str[left]))
{
left++;
}
while (left<right && !isZimu(str[right]))
{
right–;
}
if (str[left] >= ‘A’ && str[left] <= ‘Z’)
{
str[left] = str[left] +32 ;
}
if (str[right] >= ‘A’ && str[right] <= ‘Z’)
{
str[right] = str[right] +32;
}
cout << str[left] << " " << str[right] << endl;
if (str[left] != str[right])
{
return false;
}
left++;
right–;
}
return true;
}
int main()
{
string str = “A man, a plan, a canal: Panama”;
cout << ValidPalindrome(str) << endl;
system(“pause”);
return 0;
}
[LeetCode] Valid Palindrome 验证回文字符串
最新推荐文章于 2021-07-22 23:00:05 发布