首页 > 代码库 > Reverse Integer

Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

思路:

 1 class Solution { 2 public: 3     int reverse( int x ) { 4         double val = 0.0; 5         while( x != 0 ) { 6             val = 10 * val  + x % 10; 7             x /= 10; 8         } 9         if( val > INT_MAX || val < INT_MIN ) { return -1; }10         return (int)val;11     }12 };

 

Reverse Integer