A easy and clear C++ solution, beats 100%


  • 0
    F
        bool isPalindrome(int x) {
            if(x < 0) return false;
            int mod, reverse = 0, tmp = x;
            do{
                mod = tmp % 10;
                tmp = tmp / 10;
                reverse = reverse * 10 + mod;
            }while(tmp != 0);
            return reverse == x;
        }
    

  • 1

    maybe you should use long long for "reverse", because when x = 2147483648, the value of reverse may be overflow.


  • 0
    Z

    @大米 看你ID 感觉应该是中国人。。。可不可以这么理解,如果一个整数反过都overflowed了,那它自身必然overflowed,不存在这样的整数啊,所以reverse用int是安全的。


  • 0
    Z

    @zhangh-t said in A easy and clear C++ solution, beats 100%:

    @大米 看你ID 感觉应该是中国人。。。可不可以这么理解,如果一个整数反过都overflowed了,那它自身必然overflowed,不存在这样的整数啊,所以reverse用int是安全的。

    sorry,是回文数。


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.