如何使用递归从数组中找到第 i 个布尔值?

从布尔数组中查找第 i 个布尔值,例如:数组为 {true, true , false, false, true},该方法将输出显示第三个真值的 int,即 4。


我已经尝试过一些代码,它可以工作,但我需要使用递归,而不是 while 函数。


public static int check(int n, boolean[] b, boolean val){

        int i = 0;

        int count = 0;

        while(i < b.length && count <= n){

            if(b[i] == val) count++;

            i++;

        }        

        if(n == count){

            return i;

        }

        else{

            return -1;

        }

    } 


暮色呼如
浏览 134回答 3
3回答

叮当猫咪

你可以这样做:int f(int n,boolean[] b,boolean val,int i){&nbsp; &nbsp; if(i>=b.length)&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; if(b[i]==val)&nbsp; &nbsp; {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if(n==1)&nbsp; &nbsp; &nbsp; &nbsp; return i;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; return f(n-1,b,val,i+1);&nbsp; &nbsp; }&nbsp; &nbsp; return f(n,b,val,i+1);}

天涯尽头无女友

public class Recursion {private static boolean[] b = {true, true , false, false, true};private static int i = 0;private static int position = 0;public static void check( int i, boolean[] b, boolean val ) {&nbsp; &nbsp; if( i < b.length ) {&nbsp; &nbsp; &nbsp; &nbsp; if( b[i] == val ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position = i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; check( i, b, val );&nbsp; &nbsp; }}public static void main(String[] args) {&nbsp; &nbsp; boolean myChoice = true;&nbsp; &nbsp; check( Recursion.i, Recursion.b, myChoice );&nbsp; &nbsp; System.out.println( "Last " + myChoice + " position computed is " + position );}}递归是关于调用自身的方法。在上面的示例中,该方法检查“计数器”变量 i 是否小于布尔数组的长度。接下来检查数组的当前元素是否与选择匹配。如果是,则将计数器值分配给该位置(最后计算的位置)。计数器递增,并且该方法调用自身。该过程一直持续到 i 等于布尔数组的大小。此时,该方法停止调用自身。

aluckdog

实际的函数从 0 开始计数,所以我在里面传递了一个位置-1。如果 i = 0,那么很明显我们应该返回什么 - 第一个条目的索引。如果 i 大于 0,假设它是 1,那么我们将数组分成两部分:我们已经搜索到的包含第一个条目的部分,以及数组的其余部分。这可以使用 subList() 来完成。现在我们可以在数组的其余部分上使用带有 i-1 的函数,这将找到数组第二部分中第一个条目的索引。此外,我们还必须添加我们剪切掉的内容的大小,这将list.subList(0, list.indexOf(value) + 1).size()需要记住原始数组中的索引。&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; List<Boolean> list = Arrays.asList(true, true, false, false, true, true, false, false); //8&nbsp; &nbsp; &nbsp; &nbsp; int position = 4; //find index of fourth false&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(recursiveSearch(list, false, position - 1));&nbsp; &nbsp; &nbsp; &nbsp; position = 2; //find index of second true&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(recursiveSearch(list, true, position - 1));&nbsp; &nbsp; }&nbsp; &nbsp; private static int recursiveSearch(List<Boolean> list, boolean value, int i) {&nbsp; &nbsp; &nbsp; &nbsp; if(i == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return list.indexOf(value);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return list.subList(0, list.indexOf(value) + 1).size() + recursiveSearch(list.subList(list.indexOf(value) + 1, list.size()), value, i - 1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java