I Changed 'int' to 'long', no more overflow problem...


  • 0
    T

    public class Solution {
    public long reverse(int x) {
    boolean neg=false;
    if(x<0){neg=true;}
    long y=0;
    int yy=0;
    int d;
    int xx=x;
    while(x!=0){
    d=x%10;
    x=x/10;
    y=y*10+d;
    }
    if(neg==false && y>=2147483647L || neg==true && y<=-2147483648L) y=0;

        //yy=y.intValue();
        if(neg==true) return y;
        
        else return y;
    }
    

    }


Log in to reply
 

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