题解 | #判断回文串#

判断回文串

https://www.nowcoder.com/practice/b4dc0f1ee20448fca1f387fb1546f43f


#include <cctype>
class Solution {
  public:
    /**
     *
     * @param s string字符串
     * @return bool布尔型
     */
    bool isPalindrome(string s) {
        // write code here
        string cleaned;

        // 清理字符串
        for (char c : s) {
            if (isalnum(c)) {
                cleaned += tolower(c);
            }
        }

        // 双指针比较
        int left = 0, right = cleaned.length() - 1;
        while (left < right) {
            if (cleaned[left] != cleaned[right]) {
                return false;
            }
            left++;
            right--;
        }
        return true; // 处理空字符串的情况

    }
};

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 11:31
点赞 评论 收藏
分享
06-17 21:57
门头沟学院 Java
白友:噗嗤,我发现有些人事就爱发这些,明明已读不回就行了,就是要恶心人
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务