Share my C/C++ solution with detailed explanation


  • 0
    E
    int reverse(int x) {
            int p=x>0?x:-x;     
                 // p is the abs value
            long int result=0;
                 // in case of overflow, we choose long int
            while(p!=0){
                result=result*10+p%10;
                p/=10;
            }
                 //reverse the abs value
            result=(x>0)?result:-result;
                 //check if x>0
            if(result>INT_MAX||result<INT_MIN)
                return 0;
                 //check overflow
            return result;
        }

Log in to reply
 

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