Concise C++ code with long->int


  • 0
    L

    The idea is to use long for overflow checking, and sign checking is not necessary.

    class Solution {
    public:
        int reverse(int x) {
            long result = 0;
            while (x!=0) {
                result *=10;
                result += x%10;
                x /= 10;
            }
            if ((result > 2147483647) || (result < -2147483648)) {
                return 0;
            }
            return result;
        }
    };

Log in to reply
 

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