Simple and Naive solution using StringBuilder


  • 0
    Y
    public class Solution {
        public int reverse(int x) {
            if(x==0) return 0;
            while(x%10==0) x/=10;
            StringBuilder s=new StringBuilder(Integer.toString(x));
            String a;
            long b;
            if(x<0){
                s.deleteCharAt(0);
                s.append('-');
                }
            a=s.reverse().toString();
            b=Long.parseLong(a);
            return (b<Integer.MIN_VALUE||b>Integer.MAX_VALUE)? 0:(int)b;
        
            
                
        }
    }
    

Log in to reply
 

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