Solution in Java


  • 0
    R
    class Solution {
        public int reverse(int x) {
            if(x == 0 || x == Integer.MAX_VALUE) return 0;
            if((x < 10 && x > 0) || (x > -10 && x < 0)) {
                return x;
            }
            try{
                boolean neg = x < 0;
                if(neg) {
                    x *= -1;
                }
                Integer x_Int = x;
                String x_str = x_Int.toString();
                char[] x_chars = x_str.toCharArray();
                int x_length =  x_str.length();
                char[] x_rever = new char[x_length];
                for(int i = 0; i < x_length; i++) {
                    x_rever[x_length - 1 - i] = x_chars[i];
                }
                Integer r_x = Integer.valueOf(new String(x_rever));
                return neg ? (r_x *= -1) : r_x;
            }catch(Exception e){
                return 0;
            }
        }
       
    }
    

Log in to reply
 

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