Skip to content

Instantly share code, notes, and snippets.

@zac-xin
Created December 20, 2012 22:21
Show Gist options
  • Save zac-xin/4349080 to your computer and use it in GitHub Desktop.
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?
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