【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) = ...
随机推荐
- sleep() 和 wait() 有什么区别?(未完成)
sleep() 和 wait() 有什么区别?(未完成)
- 部署WCF Lib到IIS
打开VS2013,新建项目,选择WCF|WCF服务库,取名WCFWithIIS 2. 我们用演示一个计算加法的服务.简单起见,直接在IService接口中添加加法接口 3. 实现这个加法 4. 生成项 ...
- Linux系统chmod 777 误操作目录权限 - 恢复方法
小白操作Linux,手抖导致误修改了系统文件和目录权限,导致系统宕机的修复. -R / -R / test 有的是真不懂,执行了上面的第一条命令,有的是懂,但是操作太快或者粗心大意,或者有乱敲空格的恶 ...
- 处理 read_csv 报错 OSError:Initializing from file failed
1.问题发现 df=pd.read_csv("X-go报表_交易20191118.csv") print(df.info()) File "pandas/_libs/pa ...
- koa常用api文档整理
koa 支持的配置 1.应用配置是 app 实例属性,目前支持的配置项如下 配置项名称 简介 app.name 应用名称(可选项) app.env 默认为 NODE_ENV 或者 developmen ...
- javascript逻辑与(&&)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C#分隔字符串时遭遇空值
在C#中分隔字符串时,按特定字符进行分隔的时候可能会遇到空值,如何我现在传入的是Id的字符串,如:"1501,1502,1503,,1505",以逗号分隔,由于各种原因,导致传入的 ...
- sql server 游标的知识
一:认识游标 游标是SQL Server的一种数据访问机制,它允许用户访问单独的数据行.用户可以对每一行进行单独的处理,从而降低系统开销和潜在的阻隔情况,用户也可以使用这些数据生成的SQL代码并立 ...
- 2018 Nowcoder Multi-University Training Contest 1
Practice Link J. Different Integers 题意: 给出\(n\)个数,每次询问\((l_i, r_i)\),表示\(a_1, \cdots, a_i, a_j, \cdo ...
- Codeforces Educational Codeforces Round 67
目录 Contest Info Solutions A. Stickers and Toys B. Letters Shop C. Vasya And Array D. Subarray Sortin ...