0 ms C solution, Fibonacci number


  • 1

    As distinct ways' number of n stair s(n)=s(n-1)+s(n-2), in stair 0, there is only one way: start-> start, in stair 1, there also one way: start-> stair 1, move one step. set s(0)=s(1)=1, so s(2)=1+1=2;
    The array is 1 1 2 3 5 8 13...

    int climbStairs(int n)
    {
    	if (!n || 1 == n)
    		return 1;
    	int i, n1, n2, res;
    	n1 = n2 = 1;
    	for (i = 2; i <= n; ++i)
    	{
    		res = n1 + n2;
    		n1 = n2;
    		n2 = res;
    	}
    	return res;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.