Created
January 28, 2013 13:48
-
-
Save pdu/4655620 to your computer and use it in GitHub Desktop.
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? http://leetcode.com/onlinejudge#question_70
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int climbStairs(int n) { | |
| int f[3]; | |
| memset(f, 0, sizeof(f)); | |
| f[0] = 1; | |
| for (int i = 1; i <= n; ++i) { | |
| f[i % 3] = f[(i + 1) % 3] + f[(i + 2) % 3]; | |
| } | |
| return f[n % 3]; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment