public class Solution {
public int climbStairs(int n) {
int[] arr = new int[n+1];
Arrays.fill(arr,-1);
return climbStairs(n, arr);
}
public int climbStairs(int n, int[] arr) {
if(n < 0){
return 0;
}
if(n == 0){
return 1;
}
if(arr[n] != -1) return arr[n];
arr[n] = climbStairs(n-1,arr) + climbStairs(n-2,arr);
return arr[n];
}
}
Hello, welcome to Yizhe's Blog. I am Yizhe Liu, graduated from University of Arizona, with Master degree, major in Electrical and Computer Engineering. Actually, I am software guy. In my blog, many posts are about the Leetcode questions. I will post my ideas and implementations, which are accepted by Leetcode. If you have different opinions, please leave comment. I am happy to discuss with you. So lets start rocking them!!!
Wednesday, August 27, 2014
Climb Stairs
Simple question, that's why its AC rate is very high.It is almost same as the 1st question in DP chapter of CC 150. Actually, I solved it once. But, as a practice on DP, I still worked on it. Code is shown as below:
Labels:
LeetCode
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment