【LeetCode】Climbing Stairs (2 solutions)

Climbing Stairs

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?

这是最简单的DP了,递推公式为f(n)=f(n-1)+f(n-2)

因此建立vector存放中间结果即可。

class Solution
{
public:
    int climbStairs(int n)
    {
        vector<int> v;

        for(int i = 0; i <= n; i ++)
        {
            if(i == 0)
                v.push_back(1);
            else if(i == 1)
                v.push_back(1);
            else
                v.push_back(v[i-2]+v[i-1]);
        }

        return v[n];
    }
};

其实可以改进一下减少空间复杂度。

每个元素仅依赖于前两个元素,因此没有必要使用vector保存所有中间结果,只需要保存前两个。

class Solution
{
public:
    int climbStairs(int n)
    {
        if(n <= 1)
            return 1;
        int pre1 = 1;    //n==0
        int pre2 = 1;    //n==1
        for(int i = 2; i <= n; i ++)
        {
            int cur = pre1+pre2;
            pre1 = pre2;
            pre2 = cur;
        }

        return pre2;
    }
};

时间: 2024-07-28 12:22:20

【LeetCode】Climbing Stairs (2 solutions)的相关文章

【LeetCode】Climbing Stairs

Set集合的配置 数据表的创建:表关系一个员工拥有多个身份 create table EMPLOYEE ( id INT NOT NULL auto_increment, first_name VARCHAR(20) default NULL, last_name VARCHAR(20) default NULL, salary INT default NULL, PRIMARY KEY (id) ); create table CERTIFICATE ( id INT NOT NULL aut

【leetcode】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? 跳台阶,之前写过递归的,这次写个非递归的. 到第n层的方式 其实就是 到第n-1层的方式(爬一层)和到第n-2层的方式(爬两层)的和 class Solution {

【LeetCode】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

【LeetCode】Same Tree (2 solutions)

Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 解法一:递归 /** * Definition for binary tree * struct TreeNod

【LeetCode】Plus One (2 solutions)

Plus One Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 从个位数字往前扫,只要一个数字不为9,即可加1返回. 如果直到最高位仍为9,则说明结果为100…0 解法一: 可能会使用额外空间与复制

【LeetCode】Generate Parentheses (2 solutions)

Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "

【LeetCode】Sqrt(x) (2 solutions)

Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 解法一:牛顿迭代法 求n的平方根,即求f(x)=x2-n的零点 设初始值为x0,注,不要设为0,以免出现除数为0,见后. 则过(x0,f(x0))点的切线为g(x)=f(x0)+f'(x0)*(x-x0) g(x)与x轴的交点为x1=x0-f(x0)/f'(x0) 递推关系为xn+1=xn-f(xn)/f'(xn) 当收敛时即为解. class

【LeetCode】Symmetric Tree (2 solutions)

Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note:Bonus points if you could sol