Java, recursively reverse an array

后端 未结 13 1158
悲&欢浪女
悲&欢浪女 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

    void reverseArray(int[] x){
       reverse(x, 0, x.length -1);
    }
    
    void reverse(int[] x, int i, int j){
        if(i

    Test:

    int[] s = new int[]{1,2,3,4,5};
    reverseArray(s);
    System.out.println(Arrays.toString(s));//"5,4,3,2,1"
    

    Recursive, O(n), no temporary Array needed.

提交回复
热议问题