3.1—字符串—Valid Palindrome

来源:互联网 发布:淘宝菜鸟驿站 编辑:程序博客网 时间:2024/05/17 09:08
描述
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring
cases.
For example,
”A man, a plan, a canal: Panama” is a palindrome.
”race a car” is not a palindrome.
Note: Have you consider that the string might be empty? is is a good question to ask during an
interview.
For the purpose of this problem, we define empty string as valid palindrome.

#include <iostream>#include<string>#include<cmath>using namespace std;bool IsPalindrome(string str){if (str == "")return false;int begin = 0;int end = str.size() - 1;while (begin <= end){while (!(('a' <= str[begin] && str[begin] <= 'z') || ('A' <= str[begin] && str[begin] <= 'Z')))begin++;while (!(('a' <= str[end] && str[end] <= 'z') || ('A' <= str[end] && str[end] <= 'Z')))end--;if ((str[begin] == str[end]) || (abs(str[begin] - str[end]) == 32)){begin++;end--;}else{return false;}}return true;}int main(){string str = "A man ,a plan,a canal:Panama";bool flag = IsPalindrome(str);if (flag)cout << str << "是回文!" << endl;}

原创粉丝点击