Java, recursively reverse an array

后端 未结 13 1136
悲&欢浪女
悲&欢浪女 2020-12-17 16:44

I haven\'t found anything with the specific needs of my function to do this, yes, it is for homework.

So I have:

public void reverseArray(int[] x) {
         


        
13条回答
  •  我在风中等你
    2020-12-17 17:28

    Because this is your homework, I suggest an example :

    Given sequence : 1 2 3 4 5 6 7 8 9 10

    You can change to : 10 2 3 4 5 6 7 8 9 1

    After that: 10 9 3 4 5 6 7 8 2 1

    .....

    As you see, step by step, the sequence is "better" and the problem is "smaller". So, the problem you should solve to complete is :

    1) How to apply recursive call for this method. for the original, the method is : reverse(int[] a). so, after first step, you should create array b from a[2] --> a[n-1]. and using reverse(int[] b)`.

    2) after reverse b, what should we do to reverse a ? Assign values of b again back to a.

    3) stop condition : what stop condition ? You see that elements of array b less than elements of array a. So, to which step, we should stop ?

    Hope this help :)

提交回复
热议问题