70分享(80):Java实战中的回文串判断

B站影视 港台电影 2025-05-20 22:44 2

摘要:Share interests, spread happiness, increase knowledge, and leave a good future!

分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您

这里是LearningYard新学苑。

今天小编为您带来

Java实战中的回文串判断

欢迎您的访问!

Share interests, spread happiness, increase knowledge, and leave a good future!

Dear you,

this is LearningYard Academy

Today I bring you this

Palindrome string judgment in Java practice

Welcome to visit!

回文串(Palindrome)是指正着读和反着读都相同的字符串。例如,"madam" 或 "racecar" 都是回文串。在Java中判断一个字符串是否为回文串,常用的方法有几种,以下是几种常见的实现方式:

A palindrome (Palindrome) is a string that reads the same forwards and backwards. For example, "madam" or "racecar" are both palindromes. In Java, there are several common methods to determine whether a string is a palindrome. Here are some typical implementations:

1. 双指针法

1. Double pointer method

利用两个指针,一个指向字符串的开头,另一个指向字符串的结尾,然后逐步比较两个指针指向的字符,直到两个指针相遇。如果在某一时刻发现字符不相同,则字符串不是回文串。

Use two pointers, one pointing to the beginning of the string and the other to the end of the string, and then compare the characters pointed to by the two pointers step by step until they meet. If at some point the characters are found to be different, the string is not a palindrome.

public class Palindrome {

public static boolean isPalindrome(String str) {

int left = 0;

int right = str.length - 1;

while (left

if (str.charAt(left) != str.charAt(right)) {

return false;

}

left++;

right--;

}

return true;

}

public static void main(String args) {

String str = "madam";

System.out.println(isPalindrome(str)); // 输出 true

}

}

2. 使用StringBuilder的反转方法

2. Use the StringBuilder reversal method

StringBuilder 提供了一个 reverse 方法,可以直接将字符串反转,然后与原字符串比较是否相等。

StringBuilder Provides a reverse method that reverses the string directly and then compares it to the original string to see if they are equal.

StringBuilder reversed = new StringBuilder(str);

return str.equals(reversed.reverse.toString);

}

String str = "racecar";

}

}

3. 递归法

3. Recursion

通过递归逐步判断字符串的首尾字符是否相同,直到字符串的长度为1或0。

The recursive process is used to determine whether the first and last characters of the string are the same until the length of the string is 1 or 0.

public class Palindrome {

public static boolean isPalindrome(String str) {

if (str.length

return true;

}

if (str.charAt(0) != str.charAt(str.length - 1)) {

return false;

}

return isPalindrome(str.substring(1, str.length - 1));

}

public static void main(String args) {

String str = "madam";

System.out.println(isPalindrome(str)); // 输出 true

}

}

4. 用栈来实现

4. Use stack to implement

利用栈的后进先出(LIFO)特性,将字符串的字符压入栈中,然后逐个与原字符串的字符比较。

Using the stack's last-in-first-out (LIFO) feature, the characters of the string are pushed into the stack and then compared with the original string character by character.

import java.util.Stack;

public class Palindrome {

public static boolean isPalindrome(String str) {

Stack stack = new Stack;

// 将字符压入栈

for (int i = 0; i

stack.push(str.charAt(i));

}

// 比较栈中的字符和字符串的字符

if (str.charAt(i) != stack.pop) {

return false;

}

}

return true;

}

public static void main(String args) {

String str = "level";

System.out.println(isPalindrome(str)); // 输出 true

}

}

5. 使用递归比较字符

5. Use recursive comparison of characters

通过递归去比较字符串的每个字符,判断首尾是否相同。

By recursively comparing each character of the string, we can determine whether the beginning and end are the same.

return check(str, 0, str.length - 1);

}

private static boolean check(String str, int left, int right) {

if (left >= right) return true;

if (str.charAt(left) != str.charAt(right)) return false;

return check(str, left + 1, right - 1);

}

String str = "madam";

}

}

今天的分享就到这里了,

如果您对文章有独特的想法,

欢迎给我们留言。

让我们相约明天,

祝您今天过得开心快乐!


That’s all for today’s sharing.

Ifyou have a unique idea about the article,

pleaseleave us a message,

andlet us meet tomorrow.

Iwish you a nice day!

本文由learningyard新学苑原创,如有侵权,请联系我们!

翻译来源于百度翻译

部分来源于百度文库

来源:小周看科技

相关推荐