smilevchy's blog

Life & Study & Chasing

Leetcode_Climbing Stairs

原题链接: https://oj.leetcode.com/problems/climbing-stairs/

public int climbStairs(int n) {
    if (n <= 0) return 0;

    if (n == 1) {
        return 1;
    } else if (n == 2) {
        return 2;
    }

    int a = 1;
    int b = 2;
    int c = 0;

    for (int i = 3; i <= n; i++) {
        c = a + b;
        a = b;
        b = c;
    }

    return c;
}

algorithm

« Leetcode_Remove Element Leetcode_Add Binary »