【leetcode】509. Fibonacci Number
problem
solution1: 递归调用
class Solution {
public:
int fib(int N) {
if(N<) return N;
return fib(N-) + fib(N-);
}
};
solution2: 结果只与前两个数字有关。
class Solution {
public:
int fib(int N) {
if(N<) return N;
int a = ;
int b = ;
int sum =;
for(int i=; i<=N; i++)//err...
{
sum = a + b;
a = b;
b = sum;
}
return sum;
}
};
solution3: 使用数组类型数据。
但是不知道哪里有问题,为什么一直没有通过。。。哪位大神看到知道原因的麻烦告知一下下哈~
/*error...
class Solution {
public:
int fib(int N) {
vector<int> dp(N + 1);
dp[0] = 0; dp[1] = 1;
for (int i = 2; i <= N; ++i) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[N];
}
};
*/
/*error。。。
class Solution {
public:
int fib(int N) {
//int f[N+1] = {0};
vector<int> f(N+1);
f[0] = 0;
f[1] = 1;
for(int i=2; i<=N; i++)
{
f[i] = f[i-1] + f[i-2];
}
return f[N]; }
};
*/
参考
1. Leetcode_509. Fibonacci Number;
2. GrandYang;
完
【leetcode】509. Fibonacci Number的更多相关文章
- 【LeetCode】509. Fibonacci Number 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- 【Leetcode】179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 【leetcode】1189. Maximum Number of Balloons
题目如下: Given a string text, you want to use the characters of text to form as many instances of the w ...
- 【LeetCode】509. 斐波那契数
题目 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列.该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和.也就是: F(0) = 0, F(1) = 1 F(N) = ...
随机推荐
- How Many Answers Are Wrong(带权并查集)
题目 带权并查集的博客~ 题目: 多组输入数据.n,m.你不知道[1,n]内任意区间内值的和. m次询问,a b 是端点,都在n的范围以内 : v表示 [a,b]的区间内值的和.对每次询问,判断v是否 ...
- ndk学习之c++语言基础复习----面向对象编程
关于面向对象编程对于一个java程序员那是再熟悉不过了,不过对于C++而言相对java还是有很多不同点的,所以全面复习一下. 类 C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程 ...
- springboot @async 无效问题
在@SpringBootApplication启动类 添加注解@EnableAsync 异步方法使用注解@Async ,返回值为void或者Future 切记一点 ,异步方法和调用方法一定要写在不同的 ...
- a标签中的javascript:void(0)和#的区别
#包含了一个位置信息 默认的锚是#top 也就是网页的上端 而javascript:void(0) 仅仅表示一个死链接 void(0)用于执行某些处理,但是不整体刷新页面 javascript:v ...
- 将 Python 程序打包成 .exe 文件
1.简介 做了一个excel的风控模板,里面含有宏,我用python的第三方xlwings部署到linux后发现,linux环境并不支持xlwings. Python 程序都是脚本的方式,一般是在解析 ...
- change([[data],fn]) 当元素的值发生改变时,会发生 change 事件。
change([[data],fn]) 概述 当元素的值发生改变时,会发生 change 事件.大理石平台价格表 该事件仅适用于文本域(text field),以及 textarea 和 select ...
- .net SerialPort
虚拟串口并定时向虚拟串口定时发数据 http://scorpiomiracle.iteye.com/blog/653923 C#中如何使用SerialPort控件向单片机发送数据? http://zh ...
- 参数类型 (@Test层)常用参数
long lNum = 1234L; float fNum = 1.23f; double dNum = 1.23d;
- 逆元 x
逆元: 丢线 1.首先定义: 若存在正整数a,x,m,且满足ax≡1(mod m),则称a是x的乘法逆元,或称x是a的乘法逆元. Eg: 模7意义下,3的乘法逆元是5(或模7意义下,5的乘法逆元是3) ...
- Noip2013 提高组 Day2 T1 积木大赛
题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成 ...