4ms C++ code, with string


  • 0
    L
    int reverse(int x) {
        int flag = 0;
        if(x < 0)
        {
            flag = 1;
            x = abs(x);
        }
        
        string num;
        while(x)
        {
            num += x%10 + '0';
            x /= 10;
        }
        
        int revx = 0;
        revx = std::atoi(num.c_str());
        
        if(revx%10 != (num[num.size() -1]-'0'))
            return 0;
        
        if(flag)
            revx = 0-revx;
        
        return revx;
        
    }

Log in to reply
 

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