递归函数返回真/假后,java继续“返回假”行,我只是为了消除错误

保罗·西罗塔

如果数组 a 包含完全相同的数字,则数组 a 是数组 b 的排列,因此:

a{ 3, 2, 4, 1, 5 } is permutation of b{ 1, 2, 3, 4, 5, 5 };

我写了这段代码:

int x4a[] = { 3, 2, 4, 1, 5 };
int x4b[] = { 1, 2, 3, 4, 5, 5 };
System.out.println(isPermutation(x4a, x4b));//Should return true

public static boolean isPermutation(int[] a, int[] b) {
    return isPermutation(a, b, 0, 0);
}

public static boolean isPermutation(int[] a, int[] b, int indexA, int indexB) {
    
    // This code will return true if the shorter array contains all the members of
    // the longer array.

    // In this case b will be compared to a
    if (a.length < b.length) {
        if (indexB == b.length - 1 && a[indexA] == b[indexB])
            return true;
        else if (a[indexA] == b[indexB])
            isPermutation(a, b, 0, indexB + 1);
        else if (indexA == a.length - 1 && a[indexA] != b[indexB])
            return false;
        else if (indexA < a.length - 1 && a[indexA] != b[indexB])
            isPermutation(a, b, indexA + 1, indexB);

    } else {
        if (indexA == a.length - 1 && a[indexA] == b[indexB])
            return true;
        else if (a[indexA] == b[indexB])
            isPermutation(a, b, 0, indexA + 1);
        else if (indexB == b.length - 1 && a[indexA] != b[indexB])
            return false;
        else if (indexB < b.length - 1 && a[indexA] != b[indexB])
            isPermutation(a, b, indexA, indexB + 1);
    }
    // DEAFAULT to eliminate the error
    return false;
}

尽管它应该返回true,但它返回false。当我在调试器中运行时,它向我显示递归成功地深入,但是当它退出时,它会运行返回 false 的最后一行。我试图做布尔变量并返回它,但这给了我一个运行错误。

有没有办法避免写最后一行或任何其他方法来解决这个问题?

阿达什·库马尔

只需在每个 isPermutation 调用前加上 return,这样最后一行就不会执行。

if (a.length < b.length) {
        if (indexB == b.length - 1 && a[indexA] == b[indexB])
            return true;
        else if (a[indexA] == b[indexB])
            return isPermutation(a, b, 0, indexB + 1);
        else if (indexA == a.length - 1 && a[indexA] != b[indexB])
            return false;
        else if (indexA < a.length - 1 && a[indexA] != b[indexB])
            return isPermutation(a, b, indexA + 1, indexB);

    } else {
        if (indexA == a.length - 1 && a[indexA] == b[indexB])
            return true;
        else if (a[indexA] == b[indexB])
            return isPermutation(a, b, 0, indexA + 1);
        else if (indexB == b.length - 1 && a[indexA] != b[indexB])
            return false;
        else if (indexB < b.length - 1 && a[indexA] != b[indexB])
            return isPermutation(a, b, indexA, indexB + 1);
    }
    // DEAFAULT to eliminate the error
    return false;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 Java Swing Timer 只是为了延迟函数

sinon.replace vs sinon.stub只是为了替换返回值?

我需要子报表只是为了获取一些值(返回值)。是否可以隐藏子报表但保持其可运行?

内联函数 hack 只是为了使用内联 if else

如何将CLOB数据类型转换为VARCHAR?或只是为了最小化CLOB内容,因为我遇到了JAVA堆错误

Python的新手,我试图创建一个计算器只是为了好玩,并且出现了这个错误

我如何为 DataGrid 中的每一行生成复选框或任何只是为了选择多行?

返回内部函数的真假,我有点困惑

是始终需要Swift 4自定义参数标签还是只是为了消除功能歧义?

Java返回与递归函数

如何使我的递归函数返回以继续递归其他单元格

递归函数返回错误消息

递归函数返回类型错误

递归函数返回错误的值

递归函数返回错误地返回false

为什么我的递归dfs函数返回错误的值?

即使返回后函数仍继续执行

导入模块只是为了运行它

必须将我的 samba 共享 chmod 更改为 0777 感觉是错误的,只是为了让它们可写。这真的有必要吗?

是否有一个return语句只是为了满足语法错误的做法?

我怎样才能让代理类型只是为了实现一些特征?

租用Linux服务器只是为了备份我的个人数据?

Pandas Dataframe 在递归函数后返回 None ?

函数中的if语句后的“返回假”语句-目的?

为了继续上一条语句,我的返回值是多少?

函数在notifyAll()后继续,但线程在函数返回后终止

为什么我的递归函数返回None?

递归函数不返回我想要的

返回假值的指针函数