Created
December 20, 2012 22:21
-
-
Save zac-xin/4349080 to your computer and use it in GitHub Desktop.
Climb stairs.
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?
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
| public class Solution { | |
| public int climbStairs(int n) { | |
| // Start typing your Java solution below | |
| // DO NOT write main() function | |
| if( n == 0 || n == 1 || n == 2) | |
| return n; | |
| int array[] = new int[n + 1]; | |
| array[0] = 0; | |
| array[1] = 1; | |
| array[2] = 2; | |
| if( n == 0 || n == 1 || n == 2) | |
| return array[n]; | |
| for(int i = 3; i < n + 1; i++){ | |
| array[i] = array[i - 1] + array[i - 2]; | |
| } | |
| return array[n]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment