Java Solution


  • 0
    A
     public int reverse(int x) {
            if ((x < 10 && x >= 0) || (x > -10 && x < 0)) return x;
            String num = Integer.toString(x);
            String newNum = "";
            boolean isNeg = false;
            if (x < 0) {
                isNeg = true;
                num = num.substring(1);
            }
            for (int i = num.length()-1; i >= 0; i--) {
                newNum = newNum + num.charAt(i);
            }        
            if (isNeg) newNum = "-" + newNum;
            return Integer.parseInt(newNum);
        }
    

Log in to reply
 

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