Swift Solution algorithm--Stair problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.