Real 0 extra space solution


  • 0
    Y

    1, Only useful for int.
    2, Long codes with if else

    public boolean isPalindrome(int x) {
          // Integer.MAX_INT = 2147483647
          // Start if else
         if( x > 1000000000)
         {
                if(x%10 == x - 1000000000)
                {
                      x /= 10;  // move lowest digit
                      x -= 1000000000;  // move highest digit
                     if( x%10 == x - 100000000)      // num of zeros is not the same with above
                     {
                                       x /= 10;  // move lowest digit
                                       x -= 1000000000;  // move highest digit
                            if( ... )
                                  ...   // decrease the num of 0 one by one
                     }
    
                }
    
         if( x > 100000000) // decrease the num of 0 one by one
         {
                if(x%10 == x - 100000000)
                {
                      x /= 10;  // move lowest digit
                      x -= 1000000000;  // move highest digit
                     if( x%10 == x - 10000000)      // num of zeros is not the same with above
                     {
                                       x /= 10;  // move lowest digit
                                       x -= 100000000;  // move highest digit
                            if( ... )
                                  ...   // decrease the num of 0 one by one
                     }
    
                }
    
          if( ... )
                ...; 
    
          // the return statement only meets when 
          // 1, even num :  2 digits remain --> they are the same
          // 2, odd num:  1 digits remain
    
    }
    
    

Log in to reply
 

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