LeetCode :: Palindrome

来源:互联网 发布:软件定义存储产品 编辑:程序博客网 时间:2024/05/02 04:48

今天真是lucky,随机到的题目都比较简单。

这里判断回文数字。Determine whether an integer is a palindrome. Do this without extra space.

题目也是很简短,唯一值得注意的是,负数全部不是回文数字,一开始我没考虑到负数,所以没AC。

这里用 long long 来存在取反的数字,来避免越界问题。

class Solution {public:    bool isPalindrome(int x) {        bool isP = false;        if (x < 0)            return isP;        long long Reverse = 0;        long long input = x;        while (x){            Reverse = Reverse * 10 + x % 10;            x /= 10;        }        if (Reverse == input)            isP = true;        return isP;    }};
PS:  我试着把 long long 改为 int 也是没有问题的, 好像没设置这个越界case。

0 0