题解 | #判断回文串#

判断回文串

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

    }
};

全部评论

相关推荐

勉勉强强过了两题,没发挥好
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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