Reverse Integer Java : Using StringBuffer reverse

  • 0
    public int reverse(int x) {
            String reverse = x<0?"-":"";
            x = Math.abs(x);
            String stringify = new StringBuffer(Integer.toString(x)).reverse().toString();
            reverse = reverse + stringify;
            if( Integer.parseInt(reverse) > Integer.MAX_VALUE)
                return 0;
                return Integer.parseInt(reverse);

Log in to reply

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