Java solution 39 ms


  • 0
    V
    public class Solution {
        public int reverse(int x) {
            String temp ="";
           if (x<0)
           {
               temp = new StringBuilder(
                   Integer.toString(x).substring(1)
               ).reverse().insert(0, '-').toString();
               
           }
           else temp = new StringBuilder(Integer.toString(x)).reverse().toString();
            if (Long.parseLong(temp) > Integer.MAX_VALUE || Long.parseLong(temp) < Integer.MIN_VALUE) return 0;
            return Integer.parseInt(temp);    }
    }
    

    If anyone can suggest any improvements please do, both in coding style and/or efficiency.


Log in to reply
 

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