leetcode - 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?

//第一种解法
class Solution {
public:
    int climbStairs(int n) {
		int ans[100] = {1,2};
		for (int i = 2; i < n; i++)
		{
			ans[i] = ans[i-1] + ans[i-2];
		}
		return ans[n-1];
    }
};

还有一种解法,就是利用矩阵+快速幂,时间复杂度O(logn)。首先,将这个问题转化为矩阵问题。

这样,求就变成了,接下来就是用快速幂的方法解决这个问题。

class Matrix
{
public:
	Matrix();
	~Matrix();
	Matrix operator *(const Matrix &t);
	Matrix& operator=(const Matrix &t);
	int map[2][2];
};

Matrix::Matrix()
{
	map[0][0] = 1;
	map[0][1] = 1;
	map[1][0] = 1;
	map[1][1] = 0;
}
Matrix Matrix::operator*(const Matrix &t)
{
	Matrix ans;
    ans.map[0][0] = (map[0][0]*t.map[0][0]+map[0][1]*t.map[1][0]);
    ans.map[0][1] = (map[0][0]*t.map[0][1]+map[0][1]*t.map[1][1]);
    ans.map[1][0] = (map[1][0]*t.map[0][0]+map[1][1]*t.map[1][0]);
    ans.map[1][1] = (map[1][0]*t.map[0][1]+map[1][1]*t.map[1][1]);
    return ans;
}
Matrix& Matrix::operator=(const Matrix &t)
{
	for(int i = 0; i <= 1; ++i)
    {
        for(int j = 0; j <= 1; ++j)
        {
            map[i][j] = t.map[i][j];
        }
    }
    return *this;
}
Matrix::~Matrix()
{
}
class Solution {
public:
    int climbStairs(int n) {
		Matrix res,k;
		n = n + 1;
		while(n)
		{
			if(n & 1) res = res * k;
			k = k * k;
			n >>= 1;
		}
		return res.map[1][1];
    }
};
时间: 2024-10-20 03:33:32

leetcode - Climbing Stairs的相关文章

[leetcode]Climbing Stairs @ Python

原题地址:https://oj.leetcode.com/problems/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? 解题思路: 爬楼梯问题.经典的动态规划问题.每次上

[LeetCode] Climbing Stairs [24]

题目 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? 原题链接(点我) 解题思路 爬楼梯:一次可以爬1阶或者2阶,问爬n阶楼梯有多少方法? 这是个典型的斐波拉切应用场景,我们下面来分析下: 对于1阶,只有 1 种方法

LeetCode: Climbing Stairs [070]

[题目] 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阶,每次只能爬1阶或者2阶,为爬到梯子顶共有多少种爬法 [思路] 依次确定跳到每一阶上的爬法数目 这其实是一个斐波那契数列数列.假设A[

[LeetCode] Climbing Stairs (Sequence DP)

Climbing Stairs https://oj.leetcode.com/problems/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? 这题比较简单,可以使用动态规划来求解

Leetcode: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? 解析:设直至第n层有f(n)中走法,因为每次只能走1步或2步,则 f(n) = f(n-1) + f(n-2) 很明显是一个斐波那契数,f(0) = 0,

[LeetCode] 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? Show Tags 这题其实就是斐波那契数列来的. #include <iostream> using namespace std; class Solution {

LeetCode Climbing Stairs 爬楼梯

递归法(TLE代码): 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 if(n==0) 5 return 1; 6 if(n<0) 7 return 0; 8 return (climbStairs(n-1)+climbStairs(n-2)); 9 } 10 }; 动态规划法: 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 if(n==1) return

[LeetCode][JavaScript]Climbing Stairs

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? https://leetcode.com/problems/climbing-stairs/ 直接递归超时了,要动态规划. 打印前几个数

[LeetCode OJ]-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? 一共有n阶楼梯,每次只能爬1阶或2阶,问爬到顶端一共有多少种方法 方法一: 利用二叉树的思想,对于n=3时有3种方法,有几个叶子节点便有几种方法 1 void clim