写在前面

  • 字符串内置函数
  • 常用逻辑,涨知识,

题目详情

给定一个字符串,验证它是否是回文串,只考虑 [字母和数字] 字符,可以忽略字母的大小写。

说明:本题中,我们将 [空字符串] 定义为有效的回文串。

示例 1:

输入: "A man, a plan, a canal: Panama"
输出: true
示例 2:

输入: "race a car"
输出:

ac代码

class Solution
{
public:
bool isPalindrome(string s)
{
string tp;
for(char c : s)
{
if(islower(c) || isdigit(c))
tp += c;
else if(isupper(c))
tp += tolower(c);
// tp += (c+32);
}
int left = 0, right = tp.size()-1;
while(left <= right)
{
if(tp[left] != tp[right])
return false;
left++;
right--;
}
return true;
}
};
  • string 内置函数
islower(char c) 是否为小写字母
isuppper(char c) 是否为大写字母
isdigit(char c) 是否为数字
isalpha(char c) 是否为字母
isalnum(char c) 是否为字母或者数字
toupper(char c) 字母小转大
tolower(char c) 字母大转小
isalnum(char c)
  • 参考文章
  • LeetCode 125. 验证回文串 Valid Palindrome