Skip to content

Commit

Permalink
Climbing Stairs.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnieKim committed May 8, 2013
1 parent fa89217 commit a2a19da
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
27 changes: 27 additions & 0 deletions ClimbingStairs.h
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;
}
};
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ All the codes are tested using online-judge.
* Longest Palindromic Substring
* Next Permutation
* Longest Valid Parentheses
* Climbing Stairs

### Linked List
* Add Two Numbers
Expand Down

0 comments on commit a2a19da

Please sign in to comment.