递归将两个阵列合并为一个大阵列

发布于 2025-01-21 16:42:55 字数 1457 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻许诺言 2025-01-28 16:42:55

诀窍是您可以在给出的接口之外创建其他方法:

public class RecursiveMerge {
public void mergeArrays(int[] arrayOne, int[] arrayTwo, int[] mergedArray) {
    //call the internal recursive function 
    mergeArraysInternal(arrayOne, arrayTwo, 0, 0, mergedArray);
}

private void mergeArraysInternal(int[] arrayOne, int[] arrayTwo, int idxA, int idxB, int[] mergedArray) {
    int idx = idxA+idxB; //this will be the index of the mergedArray to receive a value
    if (idxA >= arrayOne.length) { //test if you're outside the bounds
        if (idxB >= arrayTwo.length) {
            return;
        }
        
        mergedArray[idx] = arrayTwo[idxB++];
    } else if (idxB >= arrayTwo.length) { //more bounds testing
        mergedArray[idx] = arrayOne[idxA++];
    } else  if (arrayOne[idxA] <= arrayTwo[idxB]) { //test for differences
        mergedArray[idx] = arrayOne[idxA++];
    } else {
        mergedArray[idx] = arrayTwo[idxB++];
    }
    //recurse
    mergeArraysInternal(arrayOne, arrayTwo, idxA, idxB, mergedArray);
}

public static void main(String[] args) {
    
    
    int[] arrayOne = {0, 2, 4, 6, 8, 10};
    int[] arrayTwo = {1, 3, 5, 7, 9};
    int[] mergeArray = new int[arrayOne.length + arrayTwo.length];
    
   new RecursiveMerge().mergeArrays(arrayOne, arrayTwo, mergeArray);
   
   System.out.println(Arrays.toString(mergeArray));
}

}

The trick is that you can create additional methods outside of the interface you were given:

public class RecursiveMerge {
public void mergeArrays(int[] arrayOne, int[] arrayTwo, int[] mergedArray) {
    //call the internal recursive function 
    mergeArraysInternal(arrayOne, arrayTwo, 0, 0, mergedArray);
}

private void mergeArraysInternal(int[] arrayOne, int[] arrayTwo, int idxA, int idxB, int[] mergedArray) {
    int idx = idxA+idxB; //this will be the index of the mergedArray to receive a value
    if (idxA >= arrayOne.length) { //test if you're outside the bounds
        if (idxB >= arrayTwo.length) {
            return;
        }
        
        mergedArray[idx] = arrayTwo[idxB++];
    } else if (idxB >= arrayTwo.length) { //more bounds testing
        mergedArray[idx] = arrayOne[idxA++];
    } else  if (arrayOne[idxA] <= arrayTwo[idxB]) { //test for differences
        mergedArray[idx] = arrayOne[idxA++];
    } else {
        mergedArray[idx] = arrayTwo[idxB++];
    }
    //recurse
    mergeArraysInternal(arrayOne, arrayTwo, idxA, idxB, mergedArray);
}

public static void main(String[] args) {
    
    
    int[] arrayOne = {0, 2, 4, 6, 8, 10};
    int[] arrayTwo = {1, 3, 5, 7, 9};
    int[] mergeArray = new int[arrayOne.length + arrayTwo.length];
    
   new RecursiveMerge().mergeArrays(arrayOne, arrayTwo, mergeArray);
   
   System.out.println(Arrays.toString(mergeArray));
}

}

不疑不惑不回忆 2025-01-28 16:42:55

递归功能的流量如该

作品,

如果完成工作,请返回答案
否则,请打电话给自己并更改输入,

例如,我有一个从0-100起的整数,我想彻底检查100个数字的数量是我的数字,我的数字是

public int DistanceFromLimit(int start,int steps){
    if(start==100){
        return steps;
    }
    else{
        return DistanceFromLimit(start+1,steps+1);
    }

   
 public static void main(String[] args) {
    DistanceFromLimit(75,0);
 }

我的程序启动时,步骤为0,启动是75。程序检查开始并发现75不是100。因此,它随后以76的启动自称为76,并以1个步骤。该程序的第二层看到76不是100 。

这将继续进行,直到最终为

100
如果完成工作,请返回一些答案。如果没有完成工作,请调用相同的功能,更改输入以表示已经完成的工作。

Recursive Functions have a flow that goes like this

DoSomeWork

If work is done, return answer
Otherwise, call myself and change the input

For example, let's say I have an integer from 0-100, and I want to reclusively check how many numbers from 100 my number is

public int DistanceFromLimit(int start,int steps){
    if(start==100){
        return steps;
    }
    else{
        return DistanceFromLimit(start+1,steps+1);
    }

   
 public static void main(String[] args) {
    DistanceFromLimit(75,0);
 }

When my program starts, steps is 0 and start is 75. My program checks start and sees that 75 is not 100. So it then calls itself with start at 76 and steps at 1. The 2nd layer of the program sees that 76 is not 100, so it calls itself with start as 77 and steps at 2.

This will keep going until steps finally is 100.

So you need to provide work and a condition of when that work is done.
If the work is done, return some answer. If the work is not done, then call the same function, changing the input to represent the work already done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文