Solution in C++,Consider number overflow


  • 0
    Q

    This is my code :
    """
    class Solution {
    public:
    int reverse(int x) {

    int residue,reverseNum=0;
    while(x){
        //若是reverseNum*10后会溢出,那么就返回0;
        if(reverseNum > INT_MAX/10 || reverseNum < INT_MIN/10){
            return 0;
        }
        residue = x%10;
        x/=10;
        reverseNum=reverseNum*10+residue;
    }
    
    return reverseNum;
    }
    

    };
    """


Log in to reply
 

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