Reverse in C++ [Accepted]


  • 0
    S

    '''
    class Solution {
    public:
    int reverse(int x) {
    int y = 0, i = 0, temp = 0;
    bool neg = false;

        if(x < 0)
            neg = true;
        
        while(x)
        {
            if(y > INT_MAX/10)
            {
                y = 0;
                break;
            }
            temp = x%10 + y*10;
            if((temp < 0 && neg == false) || (temp > 0 && neg == true)) {
                y = 0;
                break;
            } else {
                y = temp;
            }
            x /= 10;
        }
        
        return y;
    }
    

    };
    '''


Log in to reply
 

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