Java, recursively reverse an array

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

    0 讨论(0)
  • 2020-12-17 17:30

    This is probably the easiest way, not the fastest, but probably the easiest.

    A whole program would look something like this:

    public static void main(String [] args)
    {
        BackwardsArray back = new BackwardsArray();
    }
    
    public BackwardsArray()
    {
        int [] a = {1,2,3,4,5,6,7,8,9};
        printBackwards(a);
    }
    
    void printBackwards( int [] b)
    {
        print(b,b.length-1);
    }
    
    void print(int [] b, int pos)
    {
        System.out.println(b[pos]); // prints last item
        if(pos != 0)
        {
            print(b,pos-1);
        }
    }
    

    Hope it helps!

    0 讨论(0)
  • 2020-12-17 17:32

    Here is the main method:

    package main;
    
    public class Main {
        public static void main(String[] args) {
            StringOps ops = new StringOps();
            String string = "Arjun";
            // reversing the string recrusively
            System.out.println(ops.reverseRecursively(string.toCharArray(), 0));
        }
    }
    

    and here is the recursive function:

    package main;
    
    public class StringOps {
        public char[] reverseRecursively(char[] array, int i) {
            char[] empty = new char[0];
            if (array.length < 1) {
                System.out.println("you entered empty string");
                return empty;
            }
            char temp;
            temp = array[i];
            array[i] = array[array.length - 1 - i];
            array[array.length - 1 - i] = temp;
            i++;
    
            if (i >= array.length - 1 - i) {
                return array;
            } else {
                reverseRecursively(array, i);
                return array;
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-17 17:34
    public class FunWithAlgorthims {
    
    
    public static void main(final String[] args) {
    
        String[] array = {"a", "b", "c", "d"};
        printArray(array);
        revereArrayRecusrive(array, 0);
        printArray(array);
    }
    
    
    public static void revereArrayRecusrive(final String[] array, int startPointer) {
        if (startPointer >= (array.length / 2)) {
            return;
        }
        String temp = array[startPointer];
        array[startPointer] = array[array.length - 1 - startPointer];
        array[array.length - 1 - startPointer] = temp;
        revereArrayRecusrive(array, ++startPointer);
    }
    
    public static void printArray(final String[] array) {
        Arrays.stream(array).forEach(a -> System.out.print(a + " "));
        System.out.println();
    }
    

    }

    0 讨论(0)
  • 2020-12-17 17:36

    Calling reverseArray(0, n, arr) here n is length of array

    public void reverseArray(int i, int n, int [] arr)
    {
       if(i==n)
       {
         return ;
       } 
       else
       {
         reverseArray(i+1, n, arr);
         System.out.println(arr.at(i));
       }
    }
    
    0 讨论(0)
  • private static void reversePrint(int[] numbers)
    {
        if(numbers.length==0) {
            return;
        }
        int[] a = new int[numbers.length -1];
        for(int i =0;i<numbers.length-1;i++) {
            a[i] = numbers[i+1];
        }
        reversePrint(a);
        System.out.println(numbers[0]+" ");
    
    }
    
    0 讨论(0)
提交回复
热议问题