[LeetCode-7] 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 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int sign = 1, tens = 1, result = 0; 7 8 if (x < 0) { 9 x = -x; 10 sign = -1; 11 } 12 while (x > 0) { 13 result *= 10; 14 result += x % 10; 15 x /= 10; 16 } 17 return result * sign; 18 } 19 };