Title: A step has a total of n, if you can jump 1 levels at a time, you can jump 2 levels. How many total hops are in total, and the time complexity of the algorithm is analyzed. First, the topic is analyzed: The stairs have a total of n levels so when n = 1 o'clock--there is only one jump when n = 2 o'clock--there are 1, 1 or 22 jumping methods when n = 3 o'clock--there are 1, 1, 1 or 2, 1 or 1, 23 kinds of jumping .... 。。。。。。 Therefore, when n = k--There is an F (k-1) +f (k-2) method of jumping analysis here, which way the program is designed in a glance--recursion//*************************************************//
Func Jump (var n:int),int{
If there is only one step, return 1 (indicating a jumping method)
if(n = = 1) {
return 1
}
If there are two steps, return 2 (which means two jumps)
if(n = = 2) {
return 2
}
else{
var temp = Jump(n- 1) + Jump(N- 2)//recursive /c7>
return temp
}
}
//*************************************************//
Swift Solution algorithm--Stair problem