Easy to understand, Java


  • 0
    D
        public int climbStairs(int n) {
            int[] res = new int[n];
            if(n == 0 || n == 1) return 1;
            res[0] = 1;
            res[1] = 2;
            for(int i = 2;i < n;i++){
                res[i] = res[i - 1] + res[i - 2];
            }
            return res[n-1];
        }
    
    res stores all possible ways to get each step, and we return the last one as the answer
    }

Log in to reply
 

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