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)


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