Simple and clear solution using JAVA


  • 0
    W

    You could just use two variables to express previous one and previous two nums.

    class Solution {
        public int climbStairs(int n) {
            int preOne = 1, preTwo = 0;
            int cur = 0;
            for (int i=1; i<=n; ++i) {
                cur = preOne + preTwo;
                preTwo = preOne;
                preOne = cur;
            }
            return cur;
        }
    }
    

Log in to reply
 

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