-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
Author: Annie Kim, anniekim.pku@gmail.com | ||
Date: May 8, 2013 | ||
Problem: Climbing Stairs | ||
Difficulty: Easy | ||
Source: http://leetcode.com/onlinejudge#question_70 | ||
Notes: | ||
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? | ||
Solution: Clime one step from last stair or clime 2 steps from the last last stair. | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int climbStairs(int n) { | ||
int last = 1; | ||
int lastlast = 1; | ||
for (int i = 2; i <= n; i++) | ||
{ | ||
int step = last + lastlast; | ||
lastlast = last; | ||
last = step; | ||
} | ||
return last; | ||
} | ||
}; |
This file contains 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