回文数

时间复杂度O(log₁₀x)


bool isPalindrome(const int x) {
    if (x < 0) {
        return false;
    }

    if (0 == x)
        return true;

    int original = x;
    int temp = 0;
    while (original > 0)
    {
        temp = temp * 10 + (original % 10);
        original = original / 10;
    }

    return temp == x;

}

int main() {
    // 测试用例
    cout << isPalindrome(121) << endl;    // true
    cout << isPalindrome(-121) << endl;   // false
    cout << isPalindrome(10) << endl;     // false
    cout << isPalindrome(12321) << endl;  // true
    cout << isPalindrome(0) << endl;      // true
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务