Skip to content

Instantly share code, notes, and snippets.

@Ray1988
Created June 28, 2014 06:31
Show Gist options
  • Save Ray1988/db4a79c9e6afefcef9eb to your computer and use it in GitHub Desktop.
Save Ray1988/db4a79c9e6afefcef9eb to your computer and use it in GitHub Desktop.
Java
/*
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?
*/
//DP Solution
public class Solution {
public int climbStairs(int n) {
if (n<0){
return 0;
}
// record unique ways for each n
int [] unique_ways=new int[n+1];
// start point which contains only one situation
unique_ways[0]=1;
// can only move 1 step , so ways is only 1
unique_ways[1]=1;
for (int i=2; i<n+1; i++){
unique_ways[i]=unique_ways[i-1]+unique_ways[i-2];
}
return unique_ways[n];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment