15 line code using String


  • 0
    D
    public class Solution {
        public int reverse(int x) {
            String s = ""+x;
            String result = x < 0 ? "-" : "";
            int min = x < 0 ? 1 : 0;
            for(int i = s.length() - 1; i >= min; i--){
                result += s.charAt(i);
            }
            try{
                return Integer.parseInt(result);
            }catch(NumberFormatException e){
                return 0;
            }
        }
    }
    

Log in to reply
 

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