mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-02 03:05:24 +08:00
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
32 lines
800 B
Java
32 lines
800 B
Java
/**
|
|
* File: climbing_stairs_dfs.java
|
|
* Created Time: 2023-06-30
|
|
* Author: krahets ([email protected])
|
|
*/
|
|
|
|
package chapter_dynamic_programming;
|
|
|
|
public class climbing_stairs_dfs {
|
|
/* 探索 */
|
|
public static int dfs(int i) {
|
|
// dp[1] と dp[2] は既知なので、その値を返す
|
|
if (i == 1 || i == 2)
|
|
return i;
|
|
// dp[i] = dp[i-1] + dp[i-2]
|
|
int count = dfs(i - 1) + dfs(i - 2);
|
|
return count;
|
|
}
|
|
|
|
/* 階段の上り方:探索 */
|
|
public static int climbingStairsDFS(int n) {
|
|
return dfs(n);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
int n = 9;
|
|
|
|
int res = climbingStairsDFS(n);
|
|
System.out.println(String.format("%d 段の階段を上る方法は全部で %d 通り", n, res));
|
|
}
|
|
}
|