2016.6.21——Climbing Stairs
Climbing Stairs
本题收获:
1.斐波那契函数f(n) = f(n-1) + f(n -2)
题目:
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阶台阶,每次可以走2步或者1步,问有多少种方法到达顶端
思路:
leetcode:是个斐波那契函数的迭代
对于第n阶来说,有两种方法,从n-1 走 1阶 到n, 从n-2走2阶到n(刚开始想从n-2处到n 可以走1次2步 和 2次 1步,但是走1次一步不就成了n-1到n了, 重复)
代码:
class MyClass
{
public:
int clambingStairs(int n)
{
if (n == ) return ;
if (n == ) return ;
if (n == ) return ; int f2 = , f1 = , f = ;
for (int i = ; i < n; i++)
{
f = f1 + f2; //f2可以看做f(n-2)
f2 = f1; //f1看做f(n-1)
f1 = f; //f看做f(n)
}
return f;
}
};
我的测试代码:
// Climbing Stairs.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "iostream"
using namespace std; class MyClass
{
public:
int clambingStairs(int n)
{
if (n == ) return ;
if (n == ) return ;
if (n == ) return ; int f2 = , f1 = , f = ;
for (int i = ; i < n; i++)
{
f = f1 + f2; //f2可以看做f(n-2)
f2 = f1; //f1看做f(n-1)
f1 = f; //f看做f(n)
}
return f;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
int n = ;
cin >> n;
MyClass solution;
int m = ;
m = solution.clambingStairs(n);
cout << m << endl;
system("pause");
return ;
}
2016.6.21——Climbing Stairs的更多相关文章
- [LeetCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LintCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Leetcode: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM
FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM T.. = Timeline support 支持时间轴 .S. = Slice t ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- Climbing Stairs
Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...
- ”耐撕“团队 2016.3.21 站立会议3 2 1 GO!
”耐撕“团队 2016.3.21 站立会议 时间:2016.3.21 ① 17:20-17:45 ②17:55-18:10 总计40分钟 成员: Z 郑蕊 * 组长 (博客:http://www ...
- 3月3日(6) Climbing Stairs
原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
随机推荐
- javascript 进阶篇1 正则表达式,cookie管理,userData
首先,什么事正则表达式呢,其实引入概念很多时候并不能帮我们明白它到底是什么,所以我先简单描述下,正则表达式,其实就是一个记录字符串规则则的字符串,等我们看完这一部分,也就能明白它到底是什么了. 基本语 ...
- 基本数据类型用== 比较数值 引用类型用==比较的是jvm中的地址 比较数值用equal
基本数据类型用== 比较数值 引用类型用==比较的是jvm中的地址 比较数值用equal
- Digits of Factorial LightOJ - 1045(数学题?)
原文地址: https://blog.csdn.net/fenghoumilin/article/details/52293910 题意:求 n 的阶乘在 base 进制下的位数,这里有一个简单的方法 ...
- Access数据库通过ODBC导出到Oracle的两个小问题ora-24801\Ora-01401
问题描述:从access通过odbc导出到oracle出现 ora-24801 非法值 错误 与 Ora-01401 值过大的错误 问题分析:access里面的字段类型为“备注”,导入到ora ...
- Crawl(1)
爬贴吧小说. 爬取该链接中的楼主发言前10页另存为文本文件 python2.7 # *-* coding: UTF-8 *-* import urllib2 import re class BDTB: ...
- Bypass AV meterpreter免杀技巧
0x01 meterpreter简介 MetasploitFramework是一个缓冲区溢出测试使用的辅助工具,也可以说是一个漏洞利用和测试平台,它集成了各种平台上常见的溢出漏洞和流行的shellco ...
- 退出Android程序时清除所有activity的实现方法
思路: 1. 自定义ActivityList管理类,添加删除维护该list; 2.Activity Stack 类似上面: 3.singleTask定义一个Activity为该启动模式,然后当返回时, ...
- ribbion的负载均衡之端口的切换
可以说在这里被坑了很久,终于今天在大神的指导下,成功实现了负载均衡,切换不同的端口,这里来记录下,首先来看下效果图吧: 到底是怎么实现的呢?到底是如何切换的呢? 具体来讲: 几个步骤,启动服务注册中心 ...
- python代码自动补全配置及Django入门Demo
django入门代码示例小博客:https://pan.baidu.com/s/1pLjLPSv 1.自动补全功能 许多人都知道 iPython 有很好的自动补全能力,但是就未必知道 python 也 ...
- 在Kubernetes集群里安装微服务DevOps平台fabric8
转载于https://blog.csdn.net/wzp1986/article/details/72128063?utm_source=itdadao&utm_medium=referral ...