题解 | #判断回文串#

判断回文串

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; // 处理空字符串的情况

    }
};

全部评论

相关推荐

03-17 20:47
门头沟学院 Java
点赞 评论 收藏
分享
zhiyog:哈哈哈,其实是津巴布韦币
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务