java DP


  • 0

    public class Solution {
    public static int climbStairs(int n) {
    int temp[] = {0,1,2};
    if(n>=0&&n<=2){
    return temp[n];
    }
    int dp[] = new int[n+1];
    dp[0]=0;dp[1] = 1;dp[2]=2;
    if (n>2) {
    for (int i = 3; i <dp.length ; i++) {
    dp[i]=dp[i-1]+dp[i-2];
    }
    }
    return dp[n];
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.