【LeetCode算法练习(C++)】Palindrome Number

来源:互联网 发布:拼团软件排行 编辑:程序博客网 时间:2024/05/09 00:27

题目:
Determine whether an integer is a palindrome. Do this without extra space.

链接:Palindrome Number
解法:循环,与itoa类似,时间O(n)

class Solution {public:    bool isPalindrome(int x) {        int ans = 0, y = x;        if (x < 0) {            return false;        }        while (x > 0) {            if (abs(ans) > INT_MAX / 10) return false;            ans = (x % 10) + ans * 10;            x = x / 10;        }        return y == ans;    }};

Runtime: 156 ms