Reverse Integer in Java(Elementary approach)


  • 1
    A
    class Solution {
        public int reverse(int x) {
            int n;
            long rev=0;
            for(n=Math.abs(x);n>0;n=n/10){
                rev=(rev*10)+(n%10);
            }
            if(rev>2147483647||rev<-2147483648){
                return 0;
            }
            else{
            if(x<0){
                return((int)-rev);
            }
            return (int)rev;
            }
        }
    }
    

Log in to reply
 

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