Java Palindrome Tutorials
2020-12-20 14:35
标签:算法 tor 回文 private 其它 pre ping method eve Java Palindrome tutorial shows how to work with palindromes in Java. Java Palindrome教程展示了如何在Java中使用回文 Palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar. 回文报是一个单词,数字,短语或其它字符序列,它们的向前和向后读取相同,例如madam或racecar In this tutorial we show several ways to check if a string is a palidrome in Java. 在本教程中,展示了几种检查字符串是否是Java回文的方法 The In the example, we pass the original string to the 在示例中,我们将原始字符串传递给StringBuilder并将其反转。然后,我们将颠倒的字符串与原始字符串进行比较 具有for循环和charAt的Java回文 The 字符串的charAt方法返回指定索引处的char值。索引范围是0到length()-1 In the example, we build the reversed string with a for loop that goes from the end of the original string. We get the current character with 在示例中,我们使用从原始字符串末尾开始的for循环构建反向字符串。通过charAt获得当前字符 We get the number of characters in the original string with 我们获得原始字符串中具有的字符数长度 We build the reversed string by taking the characters from the end of the original string with 我们通过使用charAt从原始字符串的末尾获取字符来构建反向字符串 Java回文与toCharArray The toCharArray将字符串转换为新的字符数组 In the example, we turn the original string to an array of characters. In a while loop, we start to compare the characters from the both sides of the string; starting with the leftmost and rightmost characters. We go until the middle of the string. 在示例中,我们将原始字符串转换为字符数组。在while循环中,我们开始比较字符串两侧的字符;从最左边和最右边的字符开始, 一直走到字符串的中间 The The example uses the Java 该示例使用Java Stack容器构建反向字符串 First, we turn the string to an array of characters with 首先,使用toCharArray将字符串转换为字符数组 In the second step, we push the characters to the stack. 第二步,将字符推入堆栈 This array will hold the reversed characters. 该数组将包含反向字符 We get the reversed string now by popping the characters from the stack. 现在通过从堆栈中弹出字符来获得反向字符串 We create the reversed string from the array and compare it with the original string using 从数组创建反向字符串,然后使用equals将其与原始字符串进行比较 In this tutorial, we have check if a string is a palindrome. We have created various algorithms to check a palindrome. 在本教程中,我们检查字符串是否是回文。并创建了各种算法来检查回文 Java Palindrome Tutorials 标签:算法 tor 回文 private 其它 pre ping method eve 原文地址:https://www.cnblogs.com/PrimerPlus/p/13224594.htmlJava Palindrome with StringBuilder
StringBuilder‘s
reverse
method causes this character sequence to be replaced by the reverse of the sequence.package com.zetcode.palindrome;
public class Palindrome1 {
public static void main(String[] args) {
System.out.println(isPalindrome("radar"));;
System.out.println(isPalindrome("kayak"));
System.out.println(isPalindrome("forest"));;
}
private static boolean isPalindrome(String original) {
String reversed = new StringBuilder(original).reverse().toString();
return original.equals(reversed);
}
}
StringBuilder
and reverse it. Then we compare the reversed string to the original with equals
.Java Palindrome with for loop and charAt
String‘s
charAt
method returns the char value at the specified index. An index ranges from 0 to length() - 1
.package com.zetcode.palindrome;
public class Palindrome2 {
public static void main(String[] args) {
System.out.println(isPalindrome("radar"));
System.out.println(isPalindrome("kayak"));
System.out.println(isPalindrome("forest"));
}
private static boolean isPalindrome(String original) {
String reversed = "";
int len = original.length();
for (int i = len - 1; i >= 0; i--) {
reversed = reversed + original.charAt(i);
}
return original.equals(reversed);
}
}
charAt
.int len = original.length();
length
.for (int i = len - 1; i >= 0; i--) {
reversed = reversed + original.charAt(i);
}
charAt
.Java Palindrome with toCharArray
String‘s
toCharArray
converts the string to a new character array.package com.zetcode.palindrome;
public class Palindrome3 {
public static void main(String[] args) {
System.out.println(isPalindrome("radar"));
System.out.println(isPalindrome("kayak"));
System.out.println(isPalindrome("foreat"));
}
private static boolean isPalindrome(String original) {
char[] data = original.toCharArray();
int i = 0;
int j = data.length - 1;
while (j > i) {
if (data[i] != data[j]) {
return false;
}
++i;
--j;
}
return true;
}
}
Java Palindrome with Stack
Stack
is a last-in-first-out (LIFO) collection.package com.zetcode.palindrome;
import java.util.Stack;
public class Palindrome4 {
public static void main(String[] args) {
System.out.println(isPalindrome("radar"));
System.out.println(isPalindrome("kayak"));
System.out.println(isPalindrome("foreat"));
}
private static boolean isPalindrome(String original) {
char[] data = original.toCharArray();
Stack
Stack
container to build a reversed string.char[] data = original.toCharArray();
toCharArray
.Stack
char[] data2 = new char[data.length];
for (int i = 0; i
String reversed = new String(data2);
return original.equals(reversed);
equals
.
上一篇:web项目添加API插件
文章标题:Java Palindrome Tutorials
文章链接:http://soscw.com/index.php/essay/37447.html