Foundation - recursion

 Recursive principle

Recursion is an effective way to solve the problem, in a recursive process, the function itself as a subroutine call

You may be wondering how to call a function of itself. The trick is, every time a recursive function calls itself, it will be given to the issue dismantling sub-problems.

Recursive calls continue until the child recursive problem can be resolved without further point.

In order to ensure a recursive function does not cause an infinite loop, it should have the following attributes:

  1. A simple 基本案例(basic case)(or some cases) - can not use recursion to generate answers to terminate the program . ---Termination condition
  2. A set of rules, also referred to 递推关系(recurrence relation), can be split into the base case in all other cases. --- recurrence relations

Note that the function may have multiple locations call themselves.

 

Examples


Let's start with a simple programming problem:

Print string in reverse order.

You can easily use an iterative approach to solve this problem, which began traversing the string from the last character of the string. But how to solve it recursively it?

First of all, we can define the required function for  printReverse(str[0...n-1])which  str[0] a string that represents the first character. Then we can accomplish a given task in two steps:

  1. printReverse(str[1...n-1]): Print substring in reverse order  str[1...n-1] .
  2. print(str[0]): Print the first character in the string.

Note that we call the function itself in the first step, by definition, it makes recursive function.

example:

Private  static  void printReverse ( char [] STR) { 
  Helper ( 0 , STR); 
} 

Private  static  void Helper ( int index, char [] STR) {
   IF (STR == null || index> = str.length) {
     return ; 
  } 
  // this can be understood as the index after the first output current character index (i.e., index + 1), and the method can be understood as a macro, all characters after the back has been output, the next character output current   
  helper (index + . 1 , STR); 
  of System.out.print (STR [index]); 
}

 

Recursion example:

First, reverse text

problem:

Write a function, its role is to input string reversed. Input string to a character array char [] given in the form. 
Do not allocate additional space to another array, you must modify the input array in place, using O ( 1 extra space) to solve this problem. 
You may assume that all the characters in the array are the ASCII table printable characters.
package com.example.demo;

public class TestString0001 {

    public void reverseString(char[] s) {
        int len = s.length;

        swap(s, 0, len - 1);
    }

    private void swap(char[] s, int left, int right) {
        if (left > right) {
            return;
        }
        char temp = s[left];
        s[left] = s[right];
        s[right] = temp;

        swap(s, left+1, right-1);
    }

    public static void main(String[] args) {

        TestString0001 t = new TestString0001();
        char[] arr = {'H', 'a', 'n', 'n', 'a', 'h'};
        t.reverseString(arr);
        for (char c : arr) {
            System.out.println(c);
        }
    }
}

 

Second, the problem

Twenty-two switching node in the linked list 

given a list pairwise exchange with adjacent nodes, and return the list to the exchange. 
You can not just simply change the internal node values, but need to be the actual node exchange.
class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode temp = head;
        head = head.next;
        temp.next = head.next;
        head.next = temp;

        //下一个交换
        head.next.next = swapPairs(head.next.next);
        return head;
    }
}

 

 

 

 

Related: leetcode about recursive:

https://leetcode-cn.com/explore/featured/card/recursion-i/256/principle-of-recursion/1101/

Guess you like

Origin www.cnblogs.com/nxzblogs/p/11233304.html