Definition of palindrome string:

A palindrome means that the sequence obtained by traversing a string from left to right and from right to left is the same. That is to say, whether it is read from the left or from the right, it is the same, similar to the axisymmetric graphics learned in mathematics. For example, "abcba" and "NBAABN" are palindrome strings, while "abcd" is not a palindrome string. .

There are two common implementation ideas:

  • Two-way traversal from beginning to end, if a character is not the same, it is not a palindrome string;
  • Reverse the string and compare whether the reversed string is the same as the original string;

Example 1 - end-to-end bidirectional traversal

 private static boolean isPalindrome(String str) {
        if (str == null) {
            return false;
        }

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

        return true;
    }

You can also use a for loop to implement bidirectional traversal from end to end, as follows:

 private static boolean isPalindrome(String str) {
        if (str == null) {
            return false;
        }

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

        return true;
    }

Example 2 - Reverse String

 private static boolean isPalindrome(String str) {
        if (str == null) {
            return false;
        }

        char[] chars = str.toCharArray();
        for (int start = 0, end = chars.length - 1; start <= end; start++, end--) {
            char tempChar = chars[start];
            chars[start] = chars[end];
            chars[end] = tempChar;
        }

        return String.valueOf(chars).equals(str);
    }

The above reversed string also adopts the idea of bidirectional traversal of the beginning and the end, and realizes the exchange of two characters by means of temporary variables. In addition to the head-to-tail bidirectional traversal method, a unidirectional traversal method can also be used to reverse the string, as follows:

 private static boolean isPalindrome(String str) {
        if (str == null) {
            return false;
        }

        int length = str.length();
        char[] chars = new char[length];
        for (int i = 0; i < length; i++) {
            chars[i] = str.charAt(length - i - 1);
        }

        return String.valueOf(chars).equals(str);
    }

The above two methods are both direct traversal methods. Of course, the reverse() method of StringBuilder or StringBuffer can also be used to reverse the string, and then determine whether the string is a palindrome string.

 private static boolean isPalindrome(String str) {
        if (str == null) {
            return false;
        }

        return new StringBuilder(str).reverse().toString().equals(str);
    }

or

 private static boolean isPalindrome(String str) {
        if (str == null) {
            return false;
        }

        return new StringBuffer(str).reverse().toString().equals(str);
    }

extend:

  • Given a string, how to reverse the string?
  • Given an array, how can I achieve the inversion of the elements of the array?

Test verification

 public static void main(String[] args) {
        System.out.println(isPalindrome(null));
        System.out.println(isPalindrome(""));
        System.out.println(isPalindrome("abcd"));
        System.out.println(isPalindrome("abcba"));
    }

The output is as follows:

false
true
false
true

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


十方
234 声望433 粉丝