Java 字符串回文 – Java 数字回文示例

Java 字符串回文 – Java 数字回文示例

原文: https://howtodoinjava.com/puzzles/java-string-palindrome-number-palindrome/

回文是一个单词,词组,数字或其他单元序列,可以在任一方向上以相同的方式读取,通常是否使用逗号,分隔符或其他单词分隔符不是必需的

类似地,回文数是指如果所有数字都颠倒了就代表相同数字的那些数字(下划线可以忽略较大的数字,例如1_00_00_001)。 数字文字中的下划线是 Java7 功能中的新增内容。

1. Java 回文字符串示例

要检查回文字符串,请反转字符串字符。 现在使用String.equals()方法来验证给定的字符串是否是回文。

1.1 使用 Apache Commons StringUtils检查字符串回文

Java 中用于字符串的简单回文程序。 它也是 Java 中使用反转方法的回文程序。

public class Main 
{
    public static void main(String[] args) 
    {
        System.out.println( isPalindromeString("howtodoinjava") );		//false
        System.out.println( isPalindromeString("abcba") );				//true
    }

    public static boolean isPalindromeString(String originalString) 
    {
        String reverse = StringUtils.reverse(originalString);
        return originalString.equals(reverse);
    }
}

1.2 使用StringBuilder检查字符串回文

同样的逻辑也可以应用于StringBuffer类。

public class Main 
{
    public static void main(String[] args) 
    {
        System.out.println( isPalindromeString("howtodoinjava") );
        System.out.println( isPalindromeString("abcba") );
    }

    public static boolean isPalindromeString(String originalString) 
    {
        String reverse = new StringBuilder(originalString).reverse().toString();
        return originalString.equals(reverse);
    }
}

1.3 用for循环检查字符串回文

使用for循环charAt()方法遍历最后一个索引的字符串字符来获取反转字符串,并创建新字符串。

仅当您在 Java 中检查字符串回文而不使用反转方法时,才使用此方法。

public class Main 
{
    public static void main(String[] args) 
    {
        System.out.println( isPalindromeString("howtodoinjava") );
        System.out.println( isPalindromeString("abcba") );
    }

    public static boolean isPalindromeString(String originalString) 
    {
        String reverse = "";

        int length = originalString.length();

        for ( int i = length - 1; i >= 0; i-- )
            reverse = reverse + originalString.charAt(i);

        return originalString.equals(reverse);
    }
}

2. Java 回文数示例

为了验证给定数字是否为回文数是否为真,我们需要反转数字位数,如果两者相等或不相等,则与原始数字进行比较。

package com.howtodoinjava.puzzle;

public class PalindromeTest
{
    /**
     * Test the actual code if it works correctly
     * */
    public static void main(String[] args)
    {
        System.out.println(checkIntegerPalindrome( 100 )); 		//false
        System.out.println(checkIntegerPalindrome( 101 )); 		//true
        System.out.println(checkIntegerPalindrome( 500045 )); 	//false
        System.out.println(checkIntegerPalindrome( 50005 )); 	//true
    }

    /**
     * This function will test the equality if a number and its reverse.
     * @return true if number is palindrome else false
     * */
    public static boolean checkIntegerPalindrome(int number)
    {
        boolean isPalindrome = false;
        if(number == reverse(number))
        {
            isPalindrome = true;
        }
        return isPalindrome;
    }

    /**
     * This function will reverse a given number.
     * @return reverse number
     * */
    public static int reverse(int number)
    {
        int reverse = 0;
        int remainder = 0;
        do {
            remainder = number % 10;
            reverse = reverse * 10 + remainder;
            number = number / 10;

        } while (number > 0);
        return reverse;
    }
}

程序输出

false
true
false
true

学习愉快!