leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0007.cpp (403B)


0 class Solution { 1 public: 2 int reverse(int x) { 3 if (x == INT_MIN || x == INT_MAX) return 0; 4 5 bool neg = false; 6 unsigned long res = 0; 7 8 if (x < 0) { 9 neg = true; 10 x = -x; 11 } 12 13 do { 14 res = res * 10 + x % 10; 15 } while ((x /= 10) > 0); 16 17 if (res > INT_MAX) return 0; 18 return !neg ? res : -res; 19 } 20 };