打印所有到达第 N 级楼梯的方法,一次跳跃 1 或 2 个单位
给定一个代表N个楼梯的正整数N和一个人它在第一个楼梯,任务是打印所有到达第N个楼梯的路径,一次跳跃1 或 2 个单位。
例子:
Input: N = 3
Output:
11
2
Explanation:
Nth stairs can be reached in the following ways with the jumps of 1 or 2 units each as:
- Perform the two jumps of 1 unit each as 1 -> 1.
- Perform the two jumps of 1 unit each as 2.
Input: N = 5
Output:
1111
112
121
211
22
方法:给定的问题可以使用递归来解决。这个想法是在每个索引处同时涵盖一次或两次跳跃的情况,并打印到达第N个楼梯的所有可能方式。请按照以下步骤解决给定的问题:
- 定义一个递归函数,比如totalPossibleJumps(int N) ,它返回所有可能的跳转方式以到达第N个楼梯。
- 在递归检查的起点,基本情况如下:
- 如果N < 0 ,那么它不是一个有效的方法。所以返回一个空数组
- 如果N = 0 ,那么它是一种有效的方式。所以返回一个包含一个空格的大小为1的数组。
- 每次递归调用递归函数两次,一次为1 个单元跳转,另一次为2 个单元跳转,并分别存储结果。
- 初始化一个字符串数组,比如totalJumps来存储到达第i个索引的方式,并存储到达索引i的所有可能方式,结果存储在上述两个递归调用中。
- 在递归检查的起点,基本情况如下:
- 完成上述步骤后,将上述递归调用返回的所有可能组合打印为totalPossibleJumps(N) 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find all the ways to reach
// Nth stair using one or two jumps
vector TotalPossibleJumps(int N)
{
// Base Cases
if ((N - 1) == 0) {
vector newvec;
newvec.push_back("");
return newvec;
}
else {
if (N < 0) {
vector newvec;
return newvec;
}
}
// Recur for jump1 and jump2
vector jump1
= TotalPossibleJumps(N - 1);
vector jump2
= TotalPossibleJumps(N - 2);
// Stores the total possible jumps
vector totaljumps;
// Add "1" with every element
// present in jump1
for (string s : jump1) {
totaljumps.push_back("1" + s);
}
// Add "2" with every element
// present in jump2
for (string s : jump2) {
totaljumps.push_back("2" + s);
}
return totaljumps;
}
// Driver Code
int main()
{
int N = 3;
vector Ans = TotalPossibleJumps(N);
for (auto& it : Ans)
cout << it << '\n';
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find all the ways to reach
// Nth stair using one or two jumps
static ArrayList TotalPossibleJumps(int N)
{
// Base Cases
if ((N - 1) == 0) {
ArrayList newvec
= new ArrayList();
newvec.add("");
return newvec;
}
else {
if (N < 0) {
ArrayList newvec
= new ArrayList();
return newvec;
}
}
// Recur for jump1 and jump2
ArrayList jump1 = TotalPossibleJumps(N - 1);
ArrayList jump2 = TotalPossibleJumps(N - 2);
// Stores the total possible jumps
ArrayList totaljumps
= new ArrayList();
// Add "1" with every element
// present in jump1
for (String s : jump1) {
totaljumps.add("1" + s);
}
// Add "2" with every element
// present in jump2
for (String s : jump2) {
totaljumps.add("2" + s);
}
return totaljumps;
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
ArrayList Ans = TotalPossibleJumps(N);
for (String it : Ans)
System.out.println(it);
}
}
// This code is contributed by ukasp.
Python3
# python program for the above approach
# Function to find all the ways to reach
# Nth stair using one or two jumps
def TotalPossibleJumps(N):
# Base Cases
if ((N - 1) == 0):
newvec = []
newvec.append("")
return newvec
else:
if (N < 0):
newvec = []
return newvec
# Recur for jump1 and jump2
jump1 = TotalPossibleJumps(N - 1)
jump2 = TotalPossibleJumps(N - 2)
# Stores the total possible jumps
totaljumps = []
# Add "1" with every element
# present in jump1
for s in jump1:
totaljumps.append("1" + s)
# Add "2" with every element
# present in jump2
for s in jump2:
totaljumps.append("2" + s)
return totaljumps
# Driver Code
if __name__ == "__main__":
N = 3
Ans = TotalPossibleJumps(N)
for it in Ans:
print(it)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find all the ways to reach
// Nth stair using one or two jumps
static List TotalPossibleJumps(int N)
{
// Base Cases
if ((N - 1) == 0) {
List newvec = new List();
newvec.Add("");
return newvec;
}
else {
if (N < 0) {
List newvec = new List();
return newvec;
}
}
// Recur for jump1 and jump2
List jump1
= TotalPossibleJumps(N - 1);
List jump2
= TotalPossibleJumps(N - 2);
// Stores the total possible jumps
List totaljumps = new List();
// Add "1" with every element
// present in jump1
foreach (string s in jump1) {
totaljumps.Add("1" + s);
}
// Add "2" with every element
// present in jump2
foreach (string s in jump2) {
totaljumps.Add("2" + s);
}
return totaljumps;
}
// Driver Code
public static void Main()
{
int N = 3;
List Ans = TotalPossibleJumps(N);
foreach(string it in Ans)
Console.WriteLine(it);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
11
2
时间复杂度: O(2 N )
辅助空间: O(N)