Java O(n) time, O(1) space. Easy to Understand


  • 0
    A



    public class Solution {
    public int climbStairs(int n) {
    int a=1, b=1, c=0;
    if (n == 1 || n == 2) return n;
    for (int i=2; i<=n; i++) {
    c = a+b;
    a = b;
    b = c;
    }
    return c;
    }
    }


Log in to reply
 

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