【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) = ...
随机推荐
- 凌乱的yyy / 线段覆盖(贪心)
https://www.luogu.org/problemnew/show/P1803 题目链接 贪心,选择结束时间为关键字排序,相同时开始时间大的在前,然后for一遍比较就好了 #include& ...
- Paper Reading:Mask RCNN
Mask RCNN 论文:Mask R-CNN 发表时间:2018 发表作者:(Facebook AI Research)Kaiming He, Georgia Gkioxari, Piotr Dol ...
- LeetCode刷题--有效的括号(简单)
题目描述 给定一个只包括 ' ( ' , ' ) ', ' { ' , ' } ' , ' [ ' , ' ] ' 的字符串,判断字符串是否有效.有效字符串需满足: 左括号必须用相同类型的右括号闭 ...
- nginx 重发机制导致的重复扣款问题
问题: nginx 重发机制导制重复提交(客户还款,被扣俩笔款,前端调用一次,后端执行2次) proxy_next_upstream 语法: proxy_next_upstream error ...
- github下载慢的问题
1. 修改HOSTS文件:在“C:\Windows\System32\drivers\etc” 下的HOSTS文件,添加以下地址: 151.101.44.249 github.global.ssl. ...
- java获取一段字符串里符合日期的正则表达式的字符串
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test3 { public static v ...
- form表单 一个input时 回车自动提交
问题描述 form表单中,如果当前表单只有一个input输入框时,单击回车会自动提交当前表单. 解决方案 在当前form表单中添加一个隐藏的input, <input style="d ...
- 参数类型 (@Controller层)
@RequestMapping(path = "/listPage")@SuppressWarnings("unchecked")@BussinessLog(v ...
- 错误/异常:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/classes/beans_common.xml]...的解决方法
1.第一个这种类型的异常 1.1.异常信息:org.springframework.beans.factory.BeanCreationException: Error creating bean w ...
- 【概率论】3-6:条件分布(Conditional Distributions Part I)
title: [概率论]3-6:条件分布(Conditional Distributions Part I) categories: Mathematic Probability keywords: ...