【问题标题】:Unable to return base case value in Recursion problem无法在递归问题中返回基本案例值
【发布时间】:2020-11-04 20:10:33
【问题描述】:

编辑:感谢我得到的答案!

我刚刚开始学习数据结构,但我似乎无法弄清楚为什么基本案例不起作用。在这种方法中,我们应该拆分我们获得的数组并搜索键是否大于/小于您拆分的位置。找到下面的方法(我取出了大部分的sys.out.println,因为堆栈溢出说我有太多代码):-

public static int nSearch (int n, int a[], int fromIndex, int toIndex, int key){

      //base case
      if ((toIndex - fromIndex) <= n) 
      { // if there is only one element per split or less

          for (int i = fromIndex; i < toIndex; i++) {
        
              if (a[i] == key) {
                  System.out.println("If a[i] == key runs.");
                  return i;
              }
          }
          return -1;
      }
      
      int splitIndex = a.length/n; //2

      if (key >= a[splitIndex]) { // if key is bigger/equal to a[splitIndex], meaning the range is 2-3
          
          nSearch (n, a, splitIndex, a.length, key);
          
      }
      else {

          //else the range is 0-1
          nSearch (n, a, 0, splitIndex, key);
      }

      return 0;
  }

这是我发送测试用例时控制台返回的内容:

My Search Tester 2 is Running Now.
Array Length: - 4
Starting nSearch: - n : 2 fromIndex : 0 toIndex : 4
false
Case 1: n = 2 SplitIndex Value = 2 aLength = 4 Key value = 4
Starting nSearch: - n : 2 fromIndex : 2 toIndex : 4
true
Base Case Running
For loop Running
fromIndex : 2 toIndex : 4
i : 2 a[i] : 3 Key : 4
For loop Running
fromIndex : 2 toIndex : 4
i : 3 a[i] : 4 Key : 4
If a[i] == key runs.
Target 4 is at Index := 0

如您所见,输出显示控制进入if (a[i] == key) 块,但我不明白为什么return i 不起作用。请告诉我我做错了什么。

我不能发布测试代码,以免我的帖子主要是代码而被拒绝,但这是我正在使用的测试用例:

int arr = {1,2,3,4}
nSearch(2, arr, 0, arr.length, 4);

nSearch 方法应该是二分搜索的一种变体,如果有帮助的话。

【问题讨论】:

    标签: java if-statement recursion return-value binary-search


    【解决方案1】:

    我不明白为什么return i 不起作用

    没有理由认为return 语句不像宣传的那样工作,但在大多数情况下,程序会忽略结果返回值。

    但请注意方法末尾的return 0。您认为在什么情况下执行该命令比较合适?答:没有。我怀疑您添加它是为了清除编译器关于控制到达非 void 方法末尾的反对意见,但如果是这样,那么您应该更深入地研究:为什么控制首先会到达那个点?

    考虑前面的位:

          if (key >= a[splitIndex]) { // if key is bigger/equal to a[splitIndex], meaning the range is 2-3
              
              nSearch (n, a, splitIndex, a.length, key);
              
          }
          else {
    
              //else the range is 0-1
              nSearch (n, a, 0, splitIndex, key);
          }
    

    在每个分支中递归调用nSearch(),但是当递归调用返回时会发生什么?没什么特别的。控制从那里继续,直接到那个错误的return 0,它产生测试仪报告的0返回值。递归调用返回的任何值都将被忽略。

    您方法最后几行的这种变体应该更适合您:

      if (key >= a[splitIndex]) {          
          return nSearch(n, a, splitIndex, a.length, key);
      } else {
          return nSearch(n, a, 0, splitIndex, key);
      }
    
      // return 0;
    

    【讨论】:

      【解决方案2】:

      您应该在获得内部递归的结果后终止递归。此外,在此nSearch (n, a, splitIndex, a.length, key); 中,您将一直检查到a.length,而不管此方法调用中的实际toIndex 是什么(对于fromIndex 也是如此)。因此,以下更改应该会给您正确的答案。

      if (key >= a[splitIndex]) {          
          return nSearch(n, a, splitIndex, toIndex, key);
      } 
      else {
          return nSearch(n, a, fromIndex, splitIndex, key);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-08
        • 2014-11-27
        • 2021-02-08
        • 2022-01-19
        • 1970-01-01
        • 2017-07-11
        相关资源
        最近更新 更多