Sharing two Java solutions


  • 1
    H
    public int climbStairs(int n) {
       int result=n,one=1,two=2;
       for(int i=3;i<=n;i++)
       {
           result=one+two;
           one = two;
           two = result;
       }
       return result;
    

    }

    public int climbStairs(int n) {
        int[] memo= new int[n+1];
        return climbStairs(n,memo);
    }
    public int climbStairs(int n,int[] memo) {
        if(n<3)
        memo[n]=n;
        if(memo[n]!=0)
         return memo[n];
         memo[n]=climbStairs(n-1,memo)+climbStairs(n-2,memo);
         return memo[n];
    }

Log in to reply
 

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