My accepted 8 line of string manipulation approach for JAVA


  • 0
    E

    Easy approach using string manipulaiton:

        public static int reverse(int x) {
            try {
                Boolean isNegative = x < 0 ? true : false;
                String numberString = Integer.toString(isNegative ? -x : x);
                String newNumberString = new StringBuilder(numberString).reverse().toString();
                int reverseNumber = Integer.parseInt(newNumberString);
                return isNegative ? -reverseNumber : reverseNumber;
            } 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.