【LeetCode】125. Valid Palindrome
题目:
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? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
提示:
比较简单的题目,要注意下面三点即可:
- 忽略大小写
- 只考虑数字和英文字母
- 空字符串符合要求
代码:
class Solution { public: bool isPalindrome(string s) { if (s.size() == 0) return true; for (int i = 0, j = s.size() - 1; i < j; ++i, --j) { while (!isalnum(s[i]) && i < s.size()) ++i; while (!isalnum(s[j]) && j > 0) --j; if (tolower(s[i]) != tolower(s[j]) && i < j) return false; } return true; } };
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步