Why i got a strange result?


  • 0
    L

    This is my code and i get a correct result in my local computer. But it is not work here, where is the problem?

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

    Input: 1534236469
    Output: 1056389759
    Expected: 0


  • 0
    L

    change long to long long, since long is also 32bit as same as int.


Log in to reply
 

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