Leetcode 70. Climbing Stairs (Easy) (cpp)

来源:互联网 发布:恋人 英文 知乎 编辑:程序博客网 时间:2024/05/19 21:42

Leetcode 70. Climbing Stairs (Easy) (cpp)

Tag: Dynamic Programming

Difficulty: Easy


/*70. Climbing Stairs (Easy)You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?*/class Solution {public:    int climbStairs(int n) {    if (n < 4) return n;    int i = 4, prev1 = 2, prev2 = 3;    while (i <= n) {    int temp = prev2;    prev2 += prev1;    prev1 = temp;    i++;    }    return prev2;    }};


0 0
原创粉丝点击