【问题标题】:Seeing of two strings are reflections of each other through recursion看到两个字符串是通过递归相互反映
【发布时间】:2014-07-17 05:56:42
【问题描述】:

我的代码旨在获取两个字符串,并在忽略大小写的情况下查看它们是否是彼此的直接反射。例如,isReverse("abC", "cBA") 应该返回 true,而 isReverse("abde", "aedb") 返回 false。

我无法确定它们是否是反射。为什么递归案例不起作用?我是递归编程的新手,这是一个相当大的挑战。

    public static void main(String[] args) {
        String first = "abadcd";
        String second = "Dcdbua";
        System.out.println(isReverse(first, second));
    }

    public static boolean isReverse(String first, String second) {
        if (first.length() != second.length()) { // If string are not of equal length, impossible to be reverses
            return false;
        }

        String check1 = "" + first.charAt(first.length() - 1);
        String check2 = "" + second.charAt(0);
        if (check1.equalsIgnoreCase(check2)) { // gets the 1st char of first and last char of 2nd to see if it matches
            String temp1 = first.substring(1,first.length()-1); // shorten the string by escluding first char
            String temp2 = second.substring(0, second.length()-2); // shorten the string by last char
            isReverse(temp1, temp2);
        } 
        return true; // reaches here after goes through 
    } 

【问题讨论】:

  • 这个函数有很多问题:(1)递归什么时候结束? (2) 函数什么时候返回false? (3) 错误使用子串? (4)第一和第二互换。请至少自行修复其中一些。

标签: java string recursion


【解决方案1】:

您的代码中存在多个问题。

  • 您缺少递归。您需要在 isReverse 方法中返回对 isReverse 的调用。
  • 您需要一个成功终止(如果两个长度都为 0,则字符串彼此相反)
  • 你使用的子字符串索引不好
  • 您正在反转字符比较和子字符串之间的字符串
  • 除了停止条件之外,您需要在任何地方都返回 false

      public static void main(String[] args)
      {
        String first = "abadcd";
        String second = "Dcdbua";
        String third = "dcdabA";
        System.out.println("Should be false = " + isReverse(first, second));
        System.out.println("Should be true = " + isReverse(first, third));
      }
    
      public static boolean isReverse(String first, String second)
      {
        System.out.println("1=" + first + " / 2=" + second);
        if (first.length() != second.length())
        { // If string are not of equal length, impossible to be reverses
          return false;
        }
    
        // Stopping condition
        if (first.length() == 0 && second.length() == 0)
        {
          return true;
        }
    
        String check1 = "" + first.charAt(first.length() - 1);
        String check2 = "" + second.charAt(0);
        if (check1.equalsIgnoreCase(check2))
        { // gets the 1st char of first and last char of 2nd to see if it matches
          String temp1 = first.substring(0, first.length() - 1); // shorten the string by escluding first char
          String temp2 = second.substring(1, second.length()); // shorten the string by last char
    
          return isReverse(temp1, temp2);
        }
        return false; // reaches here after goes through
      }
    

【讨论】:

    【解决方案2】:

    这里有很多问题。

    1. 子字符串调用的索引不正确。
    2. 你的递归调用不正确:递归调用isReverse的重点是做 有返回值的东西;你忽略它。
    3. 您没有决定返回 true 的“正常”终止案例(您确实有“返回 true”,但请参阅 (2)。

    【讨论】:

      猜你喜欢
      • 2017-03-16
      • 1970-01-01
      • 2020-03-10
      • 1970-01-01
      • 2021-09-20
      • 2014-07-10
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多