简体   繁体   中英

How to reverse an array of strings recursively?

supposed to reverse an array of strings recursively.

having trouble implementing this. if i was using a for loop i would just start it at the end of the array and print out the array starting with the last element and ending with the first.

I'm not too sure how to do it recursively. i was thinking about using a swap but that idea sort of fizzled when i couldnt figure out how to change the elements that i was swapping.

any ideas or a push in the right direction would be appreciated.


this is what icame up with so far. i know its wrong, i get an error out of bounds exception which im not sure how to fix. I think im not swapping the first and last correctly. but am i getting the right idea?

this is what i came up with. a is an array. its inside a class.

// reverse an array 

public void rev() 
{
    rev(0,a.length-1);
} 

private void rev(int first, int last)
{
if(last == 0)
{
//do nothing
}

else
{
while(first != last)
{
int temp = first;
first = last;
last = temp;


System.out.print(" " + a[first]);
rev((first + 1), (last - 1));
}

}
}

made some changes and it reverses the last 3 elements but the it repeats the second element. i have no if statement that controls when it runs so shouldnt it run until left = right?

this is what i changed it to

// reverse an array 
public void rev() 
{
    rev(0,a.length-1);
} 

private void rev(int first, int last)
{
    if(last == 0)
    {
    //do nothing
    }

    else
    {


    String temp = a[first];
    a[first] = a[last];
    a[last] = temp;

    System.out.print(" " + a[first]);
    rev(first+ 1, last-1);

    }
}

The trick with recursion is to try and think of the problem in terms of a base case and then a way to reduce everything to that base case.

So, if you're trying to reverse a list then you can think of it like this:

  1. The reverse of a list of size 1 is that list.
  2. For a list of size > 1 then the first element in the output list will be the last element of the input list.
  3. The rest of the output list will be the reverse of the input list, minus the last element.

You now have your recursive definition.

Hope that helps.

the while loop is too much, since you are using recursion anyway, try it like this

private void rev(int first, int last)
{
   if(first < last)
   {
      var temp = a[first];
      a[first] = a[last];
      a[last]  = temp;
      rev(first + 1, last - 1);
   }
}

I always like having a simple public method that calls the private recursive one. That way from other places in your code you just give it the array, and don't have to worry about the other arguments. Also, this catches empty arrays, but you would still need to check for null at some point near the start. Maybe throw an exception in the public method if the array is null?

public String[] reverseArray(String[] theArray) {
    this.reverseArrayWorker(theArray, 0, theArray.length -1);
}

private String[] reverseArrayWorker(String[] theArray, int left, int right) {
    // Check your base cases first
    if (theArray.length <= 1) {
        // Array is one element or empty
        return theArray;
    } else if (left - right <= 0) {
        // If there are an odd # of items in the list you hit the center
        // If there are an even number your indexes past each other
        return theArray;
    }
    // Make the recursive call
    this.reverseArrayWorker(theArray, left + 1, right - 1);
    // Switch the two elements at this level
    String temp = theArray[left];
    theArray[left] = theArray[right];
    theArray[right] = temp;
    // Return the array up a level
    return theArray;
}

This will work too. Kinda Lisp-like solution.

public static List<String> append(String x, List<String> xs) {
    xs.add(x);
    return xs;
}

public static List<String> reverse(List<String> xs) {
    return xs.isEmpty()
            ? xs
            : append(xs.get(0), reverse(xs.subList(1, xs.size())));
}

I/O:

List          ==> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Reversed list ==> [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
public int[] reverse(int[] returnMe, int[] original, int curPos){
    if (original.length == 1){
        return original;
    }else{
        if (curPos < original.length){
            returnMe[curPos] = original[original.length - 1 - curPos];
            reverse(returnMe, original, curPos + 1);
        }else{
            return returnMe;
        }
    }
}    

Here is an example (but without A String since it is homework) but hopefully it will give you the idea.

public static List<Character> reverse(List<Character> chars) {
    return chars.isEmpty() ? chars : 
     addToList(chars.get(0), reverse(chars.subList(1, chars.length()));
}

public static T List<T> addToList(T t, List<T> ts) {
    List<T> ret = new ArrayList<T>();
    ret.addAll(ts);
    ret.add(t);
    return ret;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM