recursion

Recursion

What is Recursion?

包含兩個部分

1. Base Case: 終止條件

A base case (or cases) defined, which defines when the recursion is stopped - otherwise it will go on forever!

2. Recursive Case: 遞迴條件

A recursive case, which calls the function itself with a modified input, moving it closer to the base case.

Example

1
2
3
4
5
6
7
8
9
10
11
12
public class Recursion {
public static void main(String[] args) {
System.out.println(factorial(5));
}

public static int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
}

recursion
https://shengshengyang.github.io/2024/11/03/recursion/
作者
Dean Yang
發布於
2024年11月3日
許可協議