题解 | #牛的回文编号II# 字符串 双指针

牛的回文编号II

https://www.nowcoder.com/practice/0b576fd673834425878b99c736bb6c34

知识点

字符串 双指针

思路

按照.将字符串分成两半,将前后缀的0去掉之后,双指针判断是否是回文串即可。

AC Code(C++)

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param x string字符串 
     * @return bool布尔型
     */
    bool isPalindromeNumber(string x) {
        int idx = x.find(".");
        if (idx == string::npos) return check(x);
        return check(x.substr(0, idx)) and check(x.substr(idx + 1));
    }
    bool check(string s) {
        cout << s << endl;
        while (s.size() and s.back() == '0') s.pop_back();
        reverse(s.begin(), s.end());
        while (s.size() and s.back() == '0') s.pop_back();
        for (int i = 0, j = (int)s.size() - 1; i < j; i ++, j --) {
            if (s[i] != s[j]) return false;
        }
        return true;
    }
};

全部评论

相关推荐

点赞 评论 收藏
分享
青春运维少年不会梦到...:实习大王
点赞 评论 收藏
分享
08-27 12:02
已编辑
南京外国语学校 网络安全
再来一遍:实则劝各位不要all in华子,不要相信华为hr
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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