My easy C++ solution


  • 0
    B
     bool isPalindrome(int x) {
            int m = x, n = 0;
            if (x < 0) return false;
            while (x) {
                n = n * 10 + x % 10; 
                x = x / 10;
            }
        return (m == n);

  • 2
    L

    this code may not work on the risk of overflow with x = INT_MAX or so


  • -2
    S
    bool isPalindrome(int x) {
        
        
         if (x<0 || x>INT_MAX) 
       {
        x =0 ;
        return false ; 
       }
        
        int temp = x;
        long long res = 0;
        while(x) {
            res = res*10 + x%10;
            x /= 10;
        }
      
      
      
      if( temp == res)
      {
          
          return true;
      }
      else
     {
         
         return false;
     }
          
    }
    

  • 0
    S

    my solution is similar to your ...with a additional overflow check


  • 0
    A

    A variable of type int will never be greater than INT_MAX. In other words, "x > INT_MAX" will always return false. If negative numbers are discarded, there will never be any overflow in your solutions.

    By the way, no need for using long long, int is enough for all cases.


  • 0
    A

    In the case of x = INT_MAX it will also work, because n will never hold a value greater than INT_MAX


Log in to reply
 

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