Before answering this question, we must first understand what a palindrome is and what are the characteristics of a palindrome.

Palindromic numbers: Let n be an arbitrary natural number. If the natural numbers n1 and n obtained by reversing the digits of n are equal, then n is called a palindromic number. In layman's terms, palindrome numbers are similar to the axisymmetric figures we learn in mathematics. For example, 111 and 12321 are palindrome numbers, while 1234 is not a palindrome, and decimals have no palindrome numbers.

After understanding the definition of palindrome numbers, it is straightforward to draw conclusions about numbers in special cases, such as:

  • A negative number is not a palindrome because a negative sign - - is present before the negative number;
  • The decimal has no palindrome;
  • Except for 0, a number with a single digit of 0 is not a palindrome;
  • 0-9 are palindrome numbers;

How to do it through code? There are two common ideas:

  • Reverse the number, and compare whether the reversed number is equal to the original number (pay attention to the overflow of the reversed number range);
  • Convert the number to a string, and judge whether it is a palindrome by comparing the characters;

Example 1 - Number Inversion :

 private static boolean isPalindrome(int number) {
        if (number < 0) {
            return false;
        }

        int tmpNumber = number;
        long reversedNumber = 0;
        while (tmpNumber != 0) {
            reversedNumber = reversedNumber * 10 + (tmpNumber % 10);
            tmpNumber = tmpNumber / 10;
        }

        return reversedNumber == number;
    }
The reversed number reversedNumber adopts the long type to avoid the range overflow after the number is reversed. For example, the maximum value of int is 2147483647, and the value after normal inversion is 7463847412, but it has exceeded the range of int.

Example 2 - Number to String :

 private static boolean isPalindrome(int number) {
        String numStr = String.valueOf(number);
        
        int low = 0;
        int high = numStr.length() - 1;
        while (low < high) {
            if (numStr.charAt(low) != numStr.charAt(high)) {
                return false;
            }
            low++;
            high--;
        }

        return true;
    }

The above code is implemented using a while loop, but it can also be implemented using a for loop, as follows:

 private static boolean isPalindrome(int number) {
        String numStr = String.valueOf(number);

        int length = numStr.length();
        for (int low = 0, high = length - 1; low < high; low++, high--) {
            if (numStr.charAt(low) != numStr.charAt(high)) {
                return false;
            }
        }

        return true;
    }
By analogy: if a string is given, how to determine whether it is a palindrome? The above code example has given the corresponding solution.

Test Verification :

 public static void main(String[] args) {
        System.out.println(isPalindrome(0));
        System.out.println(isPalindrome(1));
        System.out.println(isPalindrome(101));
        System.out.println(isPalindrome(12321));
        System.out.println(isPalindrome(1147483647));
    }

The output is as follows:

true
true
true
true
false

For more knowledge points related to Java interviews, you can pay attention to the [Java Interview Manual] applet, involving Java foundation, multithreading, JVM, Spring, Spring Boot, Spring Cloud, Mybatis, Redis, database, data structure and algorithm, etc.


十方
234 声望433 粉丝