Brushing notes (6)-walking the stairs

Brushing notes (6)-walking the stairs

Title description

A frog can jump up to 1 step at a time, or jump up to 2 steps. Find the total number of jumping methods for the frog to jump on an n-level step (different order to calculate different results).

Idea: Same as above. Thinking backwards, when the frog jumps to the nth step, there are two possibilities: one is from the n-1 step, and the other is from the n-2 step.

So the total probability = (n-1 case) + (n-2 case)

class Solution {
public:
    int jumpFloor(int number) {
         if(number==0)
            return 0;
        else if(number==1)
            return 1;
        else if(number==2)
            return 2;
        else
        {
            number--;
            int a[2]={1,2};
            while(number-1!=0)
            {
                int temp=a[1];
                a[1]=a[0]+a[1];
                a[0]=temp;
                number--;
            }
            return a[1];
        }
    }
};

 

Published 36 original articles · 19 praises · 20,000+ views

Guess you like

Origin blog.csdn.net/GJ_007/article/details/105397450