Java StringBuilder


  • 0
    S
    class Solution {
        public int reverse(int x) {
            StringBuilder build = new StringBuilder(Integer.toString(x));
            boolean isNeg = false;
            if (build.charAt(0) == '-') {
                isNeg = true;
                build.deleteCharAt(0);
            }
            build.reverse();
            if (isNeg) {
                build.insert(0, "-");
            }
            String result = new String(build);
            try {
                return Integer.parseInt(result);
            } catch (NumberFormatException ex) {
                return 0;
            }
        }
    }
    

Log in to reply
 

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