Skip to content

Instantly share code, notes, and snippets.

@pdu
Created January 28, 2013 13:48
Show Gist options
  • Select an option

  • Save pdu/4655620 to your computer and use it in GitHub Desktop.

Select an option

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
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