【问题标题】:What is wrong with my method isReverse我的方法有什么问题 isReverse
【发布时间】:2015-04-26 03:29:12
【问题描述】:

编写一个名为 isReverse("word1", "word2") 的递归方法,它接受两个字符串作为参数,如果两个字符串包含 相同的字符序列,但顺序相反,忽略大小写,否则返回 false。 比如调用:

isReverse("Desserts", "Stressed") 

将返回 true。 【所以压力大的时候吃甜点?】 Null、empty 和一个字母的字符串也将返回 true(如果两个参数的值相同)。 这是家庭作业,我无法使此代码正常工作。无论我做什么,它都会返回 true。

public static boolean isReverse(String word1, String word2) 
{
    if(word1 == null || word2 == null)
    {
        if(word1!= null && word2 != null)
        {
            return false;
        }
        return false;
    }
    else if(word1.length() == word2.length())
    {
        String firstWord = word1.substring(0, word1.length());
        String secondWord = word2.substring(word2.length()-1);
        if (firstWord.equalsIgnoreCase(secondWord))
        { 
            return isReverse(word1.substring(0, word1.length()), word2.substring(word2.length() - 1));
        }
    }
    return true;
}

【问题讨论】:

  • String 或 StringBuffer 类有反向方法
  • @SrinathGanesh - StringBuilder 有逆向,但我们必须假设他没有被教过 Builder 类。我已经完成了 3 年的大学生活而没有被提及。
  • 鉴于这是作业,我猜想使用内置函数完成 95% 的作业是禁止的。
  • 你的第二个单词只有 1 个字符

标签: java recursion


【解决方案1】:

首先,你有这个设置,所以它只会在两个单词都为空时返回 false;如果它们不为空,则您将重新调用该方法(如果长度相等),该方法将返回 true。

private static boolean isReverse(String a, String b) {
    // make sure the strings are not null
    if(a == null || b == null) return false;

    // If the lengths are not equal, the strings cannot be reversed.
    if(a.length() != b.length()) {
        return false;
    }

    // Convert string b to an array;
    char[] bArray = b.toCharArray();

    // Create an array to write bArray into in reverse.
    char[] copy = new char[bArray.length];

    // Iterate through bArray in reverse and write to copy[]
    for(int i = bArray.length; i < 0; i--) {
        copy[bArray.length - i] = bArray[i];
    }

    // Convert copy[] back into a string.
    String check = String.valueOf(copy);

    // See if they reversed string is equal to the original string.
    if(check.equalsIgnoreCase(a)) {
        return true;
    } else {
        return false;
    }
}

【讨论】:

    【解决方案2】:

    你在说

        if (firstWord.equalsIgnoreCase(secondWord))
        { 
            return isReverse(word1.substring(0, word1.length()), word2.substring(word2.length() - 1));
        }
    

    没关系。但是如果第一个单词不等于第二个单词怎么办

    它通过并返回true。

    你需要添加一个

    else
        return false;
    

    我还要补充一点,您的空值检查将不起作用。

        if(word1!= null && word2 != null)
        {
            return false;
        }
    

    没有用,因为您已经处于 if 状态,仅当 word1 或 word2 为空时才会发生这种情况。所以这里不能为null和null。

    如果你做到了,它会起作用

        if(word1 == null && word2 == null)
        {
            return true;
        }
    

    【讨论】:

      【解决方案3】:

      这是一个练习吗?递归似乎不是这里的最佳选择。不管怎样,你只是修剪一个词,为什么?如果您希望比较每个递归调用中的每个字符,则必须修剪这两个单词。而且您甚至没有将修剪后的单词作为参数传递给递归函数!

      您缺少的基本内容是基本案例。什么时候递归必须返回?在您的情况下,您在递归的每一步都减少了每个字符串的大小,因此您必须有一个基本情况来检查大小是否为 1。

      希望这段代码能让你头脑清醒:

      public static boolean isReverse(String word1, String word2) {
          if (word1 == null || word2 == null) {
              return false;
          }
          if (word1.length() == 1 && word2.length() == 1) {
              //Used equals just for fast compare
              return word1.equals(word2);
          } else if (word1.length() == word2.length()) {
              if (word1.charAt(0) == word2.charAt(word2.length() - 1)) {
                  String firstWord = word1.substring(1, word1.length());
                  String secondWord = word2.substring(0, word2.length() - 1);
                  System.out.printf("Trimmed %s, %s to %s, %s\n", word1, word2, firstWord, secondWord);
                  return isReverse(firstWord, secondWord);
              } else {
                  //Characters didn't matched
                  return false;
              }
          } else {
              //Lenght doesn't match
              return false;
          }
      }
      

      【讨论】:

        【解决方案4】:

        首先,我使用递归反转了一个字符串(我取了 word1)。然后如果两个字符串的结果设置为 true,则与第二个字符串进行比较。

        public static boolean isReverse(String word1, String word2)
        {
            boolean result  = false;
            //check null to avoid null pointer exception
            if(word1 == null | word2 == null){
                result = false;
            }else if(word1.length() == word2.length()){
        
                 word1 = reverseString(word1);
                 if(word1.equalsIgnoreCase(word2)){
                     result = true;
                 }
            }
            return result;
        
        }   
        
        static String reverse = "";
        public static String reverseString(String str){
        
            if(str.length() == 1){
                reverse+=str;
        
            } else {
                reverse += str.charAt(str.length()-1)
                        +reverseString(str.substring(0,str.length()-1));
            }
            return reverse;
        }
        

        【讨论】:

        • 你能解释一下你在这里做了什么吗?仅代码的答案往往不是很容易解释。
        猜你喜欢
        • 1970-01-01
        • 2014-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多