Java simple string manipulation approach(8line)


  • 0
    E

    Java string manipulation approach

        public static int reverse(int x) {
            if (x <= Integer.MAX_VALUE && x >= Integer.MIN_VALUE) {
                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;
            } else {
                return 0;
            }
        }
    

    I think there is an error for their test cases when x is too long


Log in to reply
 

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