Java, recursively reverse an array

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

    psuedo code

    function revarray(a[1...n])
      if a.length == 1 or a.length == 0
        do nothing 
      # return a
      else
         swap a[1] and a[n]
         revarray(a[2...n-1])
      # return a (The function will not return anything but the contents of a are changed)
    
    0 讨论(0)
  • 2020-12-17 17:21

    //We are just doing an operation here and calling a helper method.

    public void reverseArray(int[] nums){
      int[] hold = new int[nums.length]; //just so it will take this argument
      int[] reversed = recurReverseArray(nums, hold, nums.length - 1, 0);
      nums = reversed; //not returning just changing nums to be reversed.
    }
    public int[] recurReverseArray(int[] nums, int[] reverse, int end, int start){
      if(end == 0 && start == nums.length - 1){
      reverse[start] = nums[end];
      return reverse; //the way out.
      }
      reverse[start] = nums[end];
      return recurReverseArray(nums, reverse, end - 1, start + 1);
    }
    
    0 讨论(0)
  • 2020-12-17 17:23

    Try something as below:

    public void reverseArray(int[] x) {
        if(x.length ==2){
          //if two elements, swap them
          int first = x[0];
          x[0] = x[1];
          x[1] = first;
        }else if(x.length > 2){
          //swap first and last
          int first = x[0];
          x[0]= x[x.length-1];
          x[x.length-1] = first;
          //create a copy of middle elements
          int [] copy = new int[x.length-2];
          System.arraycopy( x, 1, copy, 0, x.length-2);
          //recursive call for middle elements
          reverseArray(copy);
          //place the reversed elements back in the original array
          System.arraycopy( copy, 0, x, 1, copy.length);
        }
    }
    
    0 讨论(0)
  • 2020-12-17 17:24

    Since there's been no statement that loops cannot be used:

    void reverseArray(int[] x) {
        if (x != null) {
            for (int i = 0; i < length.x / 2; i++) {
                int j = (length.x - 1) - i;
                int temp = x[i];
                x[i] = x[j];
                x[j] = temp;
             }
             reverseArray(null);
         }
    }
    

    Probably the fastest of the lot.

    0 讨论(0)
  • 2020-12-17 17:26
    public class RecursiveArray {
    
    
       public static int[] backWardArray(int[] arr, int start, int end) {
    
           if (start < end) {
               int temp = arr[start];
               arr[start] = arr[end];
               arr[end] = temp;
               backWardArray(arr, start + 1, end - 1);
           }
           return arr;
       }
    
        public static void main(String[] args) {
            int [] arr = {12,4,6,8,9,2,1,0};
        int [] reversedArray= backWardArray(arr, 0, arr.length-1);
        //loop through the reversed array
            for (int i: reversedArray) {
                System.out.println(i);
            }
        }
    
        public RecursiveArray() {
        }
    }
    
    0 讨论(0)
  • 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<j){//Swap
           int tmp = x[i];
           x[i] = x[j];
           x[j] = tmp;
           reverse(x, ++i, --j);//Recursive
        }   
    }
    

    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.

    0 讨论(0)
提交回复
热议问题