1

我的任务是为一个测试用户输入的回文的类编写方法。该类必须有一个递归方法,并且该方法必须调用一个辅助方法来删除空格、标点符号并忽略大小写。

我有两个工作类来做这些事情,但我想知道哪个结构效果更好,哪个辅助方法实际上适合辅助方法的描述。

这是第一堂课:

public class RecursivePalindrome
{

public boolean Palindrome(String s)
{
    return PalindromeHelper(s);
}

public boolean PalindromeHelper(String s)
{
    String a = s.toLowerCase();    //Converts any capital letters to lowercase beforte analyzing the string
    a = a.replaceAll(" ", "");    //Removes any and all spaces in the string

    for(int i = 0; i < a.length(); i++)     //Removes punctuation by using isLetter method from Character Class
    {
        if(Character.isLetter(a.charAt(i)) == false)
            a = a.replace(a.substring(i, i+1), "");
    }

    if(a.length() == 0 || a.length() == 1)
        return true;
    else if(a.charAt(0) == (a.charAt(a.length() - 1)))
        return PalindromeHelper(a.substring(1, a.length() - 1));
    else
        return false;
}
}

第二个:

public class Recurs
{

public boolean Palindrome(String s)
{
    String l = PalindromeHelper(s);

    if(l.length() == 0 || l.length() == 1)
        return true;
    else if(l.charAt(0) == (l.charAt(l.length() - 1)))
        return Palindrome(l.substring(1, l.length() - 1));
    else
        return false;
}

public String PalindromeHelper(String s)
{
    s = s.toLowerCase();    //Converts any capital letters to lowercase before analyzing the string
    s = s.replaceAll(" ", "");    //Removes any and all spaces in the string

    for(int i = 0; i < s.length(); i++)     //Removes punctuation by using isLetter method from Character Class
    {
        if(Character.isLetter(s.charAt(i)) == false)
            s = s.replace(s.substring(i, i+1), "");
    }
    return s;
}
}
4

2 回答 2

0

A couple of things before we get to the code...

A "helper" method is more usually called a utility method, which is a stateless piece of code - being stateless:

  • it should be declared as static

Adhering to java naming conventions is a great idea, so:

  • method names start with a lowercase letter
  • boolean methods start with is if reasonable to do so

So, your "helper" method should look like this:

private static String clean(String s) {
    return s.toLowerCase().replaceAll("[^a-z]", "");
}

This method does everything your method does, but in a fraction of the code.

Because your main method is also stateless, it too should be static, unless it is required to be an instance method because of class hierarchy or interfaces etc.

Thus, your main method should be:

public static boolean isPalindrome(String s) {
     return isPalindromeClean(clean(s));
}

private static boolean isPalindromeClean(String s) {
    return s.length() < 2 || a.endsWith(s.charAt(0)) && 
        isPalindromeClean(l.substring(1, l.length() - 1));
}

Again, one line of code does it all. Some things to note:

  • your code calls the "clean" method every recursion, but by creating a second method, I avoid this inefficiency.
  • the use of endsWith() to both simply and clarify the condition
  • the use of a single simple return statement that encapsulates the logic

The whole class becomes the following, with just 3 lines of actual code.

public class Recurse {

    public static boolean isPalindrome(String s) {
         return isPalindromeClean(clean(s));
    }

    private static boolean isPalindromeClean(String s) {
        return s.length() < 2 || a.endsWith(s.charAt(0)) && 
          isPalindromeClean(l.substring(1, l.length() - 1));
    }

    private static String clean(String s) {
        return s.toLowerCase().replaceAll("[^a-z]", "");
    }
}

I wouldn't bother even having the clean method - I would simply in-line it like this:

public static boolean isPalindrome(String s) {
    return isPalindromeClean(s.toLowerCase().replaceAll("[^a-z]", ""));
}

But if you've been set an assignment that says you have to create it then you're stuck with it. I would show this alternative though.

Usually, the more elegant the code, the less there is of it.

于 2012-12-22T10:46:55.033 回答
0

我会这样写。

class RecursivePalindrome
{

    public boolean Palindrome(String s)
    {
        //Think about using a stringbuilder instead of a string.

        String a = s.toLowerCase(); // Converts any capital letters to lowercase
                                    // beforte analyzing the string
        a = a.replaceAll(" ", ""); // Removes any and all spaces in the string

        for (int i = 0; i < a.length(); i++) // Removes punctuation by using
                                             // isLetter method from Character
                                             // Class
        {
            if (Character.isLetter(a.charAt(i)) == false)
                a = a.replace(a.substring(i, i + 1), "");
        }
        return validatePalindrome(a);
    }

    public boolean validatePalindrome(String s)
    {
        if (s.length() == 0 || s.length() == 1)
            return true;
        else if (s.charAt(0) == (s.charAt(s.length() - 1)))
            return PalindromeHelper(s.substring(1, s.length() - 1));
        else
            return false;
    }
}
于 2012-12-22T06:08:34.073 回答