Leetcode Climbing Stairs

Source: Internet
Author: User

You is climbing a stair case. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In what many distinct ways can you climb to the top?

Just beginning to see the problem is also very annoying confused, do not know where to start, and then think of a way, with the arrangement of the way, found that 1-43 is OK, to 44 is wrong

Then find the answer to find the answer, said the Fibonacci sequence.

Like this law-finding topic is usually in the form of f (n), we find the relationship between F (n) and F (n-1).

To go to the N-order first to go to the n-1 or n-2, because one can only go one or second order.

Go to the n-1 order has F (n-1) method, go to N-2 Order has F (n-2) method, so f (n) =f (n-1) +f (n-2),

So the question is, is there a case of F (n-1) and F (n-2) repetition? Certainly not, the order of the two are different.

Continue to verify conjecture

When N=1, Ways=1

When n=2, there are [2] [one] of the two cases, ways=2

When n=3, there are [1,1,1] [three] [2,1] cases, ways=3

When n=4, there are [1,1,1,1] [2,2] [1,1,2] [1,2,1] [2,1,1] five cases, ways=5

When n=5, there are [1,1,1,1,1] [2,2,1] [2,1,2] [1,2,2] [1,1,1,2] [1,1,2,1] [1,2,1,1] [2,1,1,1] eight cases, ways=8

The code is as follows

    public static int Climbstairs (int n)    {        if (n==1)            return 1;        if (n==2)            return 2;        int [] ns=new int [n+1]; ns[0]=1; ns[1]=1; for (int i=2;i<=n;i++) {ns[i]=ns[i-1]+ns[i-2];} RE Turn ns[n];}          

Note: Another common expression of the Fibonacci sequence is the "raw rabbit Problem". Before a lot of school recruit written questions will appear this topic, so find a job students can pay more attention to this series. Its algorithm implementation is easy to implement with recursion. I hope to be of some help to you. Thank you.

In addition, I use the code of the permutation combination, the idea is to find out how many 2 order, and then arrange the combination

For example 7, the possible combination is No 2 order, a 2 order, 2 2 order, 3 2 order

Each case is arranged in combination, then added, and when the last commit, there is an error at the time of n=44!

    public static int Climbstairs (int n) {        int t=n/2;        int sum=0;        for (int i=0;i<=t;i++)        {            sum+=c (i, N-i);        }        return sum;} public static int C (int cnt,int sum) {int original=sum; if (cnt>sum) return 0; i NT result=1; int i=1; while (i<=cnt) {result=result* (sum--)/i; i++;}//system.out.println ( "C (" +cnt+ "," +original+ ") =" +result "; return result;}               

Leetcode Climbing Stairs

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.