Reverse integer Java


  • 0
    B
    class Solution {
        public int reverse(int x) {
            try {
                if (x >= 0) {
                    return Integer.parseInt(new StringBuilder(Integer.toString(x)).reverse().toString());
                } else {
                    return -Integer.parseInt(new StringBuilder(Integer.toString(Math.abs(x))).reverse().toString());
                }
            } catch (Exception ignored) {
                return 0;
            }
        }        
    }``

Log in to reply
 

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