递归函数:在Java中检查回文 [英] Recursive Function : Check for palindrome in Java

查看:103
本文介绍了递归函数:在Java中检查回文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个检查字符串是否为回文符的类.我有两个问题.

I have a class that checks whether a string is a palindrome or not. I have two questions.

1)这是检查回文症最有效的方法吗? 2)可以递归实现吗?

1) Is this the most efficient way to check for palindrome? 2) Can this be implemented recursively?

public class Words {

    public static boolean isPalindrome(String word) {
    String pal = null;
    word = word.replace(" ", "");
    pal = new StringBuffer(word).reverse().toString();
    if (word.compareTo(pal) == 0) {
        return true;
    } else {
        return false;
    }

    }

}

有一个测试班来测试这个……怀疑它的必要性,但是无论如何,只要有人愿意尝试一下就可以帮助我解决上述两个问题中的任何一个……

Have a test class to test this... Doubt its needed but here it is anyways if anyone cares to try it out to be able to help me with any of the two questions above...

public class testWords {

    public static void main(String[] args) {
    if (Words.isPalindrome("a") == true) {
        System.out.println("true");
    } else {
        System.out.println("false");
    }
    if (Words.isPalindrome("cat") == true) {
        System.out.println("true");
    } else {
        System.out.println("false");
    }
    if (Words.isPalindrome("w o    w") == true) {
        System.out.println("true");
    } else {
        System.out.println("false");
    }
    if (Words.isPalindrome("   a  ") == true) {
        System.out.println("true");
    } else {
        System.out.println("false");
    }
    if (Words.isPalindrome("mom!") == true) {
        System.out.println("true");
    } else {
        System.out.println("false");
    }

    }

}

在此先感谢您的帮助和输入:)

thanks in advance for any help and or input :)

推荐答案

要递归实现回文检查",必须比较第一个和最后一个字符是否相同.如果它们不相同,则字符串肯定不是回文.如果它们相同,则字符串可能是回文,您需要将第二个字符与第二个字符和最后一个字符进行比较,依此类推,直到您要在字符串中保留的字符数严格少于2个为止.

To implement a 'palindrome check' recursively, you must compare if the first and last characters are the same. If they are not the same the string is most certainly not a palindrome. If they are the same the string might be a palindrome, you need to compare the 2nd character with the 2nd to last character, and so on until you have strictly less then 2 characters remaining to be checked in your string.

递归算法如下:

public static boolean isPalindrome(String word) {
  //Strip out non-alphanumeric characters from string
  String cleanWord = word.replaceAll("[^a-zA-Z0-9]","");
  //Check for palindrome quality recursively
  return checkPalindrome(cleanWord);
}
private static boolean checkPalindrome(String word) {
  if(word.length() < 2) { return true;  }
  char first  = word.charAt(0);
  char last   = word.charAt(word.length()-1);
  if(  first != last  ) { return false; }
  else { return checkPalindrome(word.substring(1,word.length()-1)); }
}

  • 请注意,我的 recursion 方法不是最有效的方法,但是 简单易懂

    • Note, that my recursion method is not most efficient approach, but simple to understand

      Marimuthu Madasamy 具有更有效的递归 >方法,但很难理解

      Marimuthu Madasamy has a more efficient recursive method, but is harder to understand

      这篇关于递归函数:在Java中检查回文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆