"Reverse Integer" Solution in C++


  • 0
    M
    #include <cstdint>
    #include <limits>
    using namespace std;
    
    class Solution {
    public:
        int reverse(int x) {
            int64_t sign = 1, xx = x, r = 0;
            if (xx < 0) {
                sign = -1;
                xx = -xx;
            }
            while (xx) {
                r *= 10;
                r += xx % 10;
                xx /= 10;
            }
            r *= sign;
            if (r < numeric_limits<int>::min() || r > numeric_limits<int>::max()) return 0;
            return r;
        }
    };
    

Log in to reply
 

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