Solution in c++ without using long int


  • 0
    N
    class Solution {                                                                                                                                                        
    public:                                                                                                                                                                 
      int reverse(int num) {                                                                                                                                                
        int sign = num < 0 ? -1 : 1;                                                                                                                                        
        num = num*sign;                                                                                                                                                     
        int imax = std::numeric_limits<int>::max();                                                                                                                         
        std::cout << imax << std::endl;                                                                                                                                     
        int rev = 0;                                                                                                                                                        
        while (num != 0) {                                                                                                                                                  
          if (rev <= ((imax - num%10)/10)) { rev = rev*10 + (num % 10); }                                                                                                   
          else return 0;                                                                                                                                                    
          num = num/10;                                                                                                                                                     
        }                                                                                                                                                                   
        return rev*sign;                                                                                                                                                    
      }                                                                                                                                                    
    };      
    

Log in to reply
 

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