Transferred from: https://www.cnblogs.com/xiaosen992608/p/4037682.html
Recursion:
The concept of recursion: The method itself calls itself a recursive.
Classification of recursion:
Indirect recursion: Method A calls Method B, method B calls method C, method C calls method A. Direct recursion: Method A calls method A. Common
Considerations for recursion:
Recursion must be exported: the condition of ending recursion. Not too many recursion times.
If recursion does not end, an error will be returned.
Java.lang.StackOverflowError: Stack Memory overflow error
Recursive memory overflow hidden causes:
Method keeps going into the stack without the stack, resulting in insufficient stack memory.
Three conditions for recursion:
Recursive forward segment recursive return segment of boundary condition
Recursion advances when boundary conditions are not met, and returns recursively when the boundary conditions are met.
The following is a sample program to illustrate:
1. Factorial
public class Test01 {public static void Main (string[] args) { System.out.println (f (5)); } public static int F (int n) { if (1 = = N) return 1; else return n (n-1); } }
2. Fibonacci Sequence
public static int F (int n) { if (n = = 1 | | n = = 2) { //recursive termination condition return 1; Simple Scenario } return Fibonacci (n-1) + Fibonacci (n-2)///same repeating logic to reduce the size of the problem}
3. The judgment of the string of the back character
public static Boolean ispalindromestring_recursive (String s) { int start = 0; int end = S.length ()-1; if (end > Start) { //recursive termination condition: Two pointer moves in opposite directions, when start exceeds end, complete the judgment if (S.charat (start)! = S.charat (end)) { return false; } else{ //Recursive call, narrowing the size of the problem return ispalindromestring_recursive (s.substring (start+1). substring (0, end-1));} } return true;}
User Li Jigang (https://www.zhihu.com/question/20507130/answer/15551917) A vivid explanation of recursion and circulation:
Recursion: You open the door in front of you and see there is a door in the house. You walk over, find the key in your hand can open it, you push the door, you find there is a door, you continue to open it. After a few times, you open the door in front of you, found that there is only one room, there is no door. Then, you start the original road back, every walk back to a room, you several times, walk to the entrance, you can answer the end you use this you put the key to open a few door.
Loop: You open the door in front of you and see there is a door in the house. You walk over and find the key in your hand to open it, you push the door, and you find there is a door (if the front two doors are the same, then this door and the first two doors are the same; if the second door is smaller than the first door, then the door is smaller than the second door, and you continue to open the door and keep going until you open all the doors.) But the people at the entrance are always waiting for you to go back and tell him the answer.
Recursive thinking in Java