Just check for the overflows


#1
int Solution::reverse(int A) {
    
    int answer=0;
    
    if(A<0)//negative
    {
        while(A)
        {
            if(answer<(INT32_MIN/10) || (answer==(INT32_MIN/10) && (A%10)<(INT32_MIN-(answer*10))))
                return 0;
            
            answer=(answer*10)+(A%10);
            A=A/10;
        }
    }
    else//positive
    {
        while(A)
        {
            if(answer>(INT32_MAX/10) || (answer==(INT32_MAX/10) && (A%10)>(INT32_MAX-(answer*10))))
                return 0;
            
            answer=(answer*10)+(A%10);
            A=A/10;
        }    
    }
    
    return answer;
    
}

#2

check the result for overflow, I made the mistake of checking the input !


#3

Can you please explain this line?
if(answer<(INT32_MIN/10) || (answer==(INT32_MIN/10) && (A%10)<(INT32_MIN-(answer*10))))