【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) = ...
随机推荐
- 部署WCF Lib到IIS
打开VS2013,新建项目,选择WCF|WCF服务库,取名WCFWithIIS 2. 我们用演示一个计算加法的服务.简单起见,直接在IService接口中添加加法接口 3. 实现这个加法 4. 生成项 ...
- P1006 传纸条[棋盘DP]
题目来源:洛谷 题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接 ...
- 从项目开始的Java开发学习
积累了一些项目中见到的代码,希望见一次之后自己也能写出来. 一.通过cxf JaxWsDynamicClientFactory进行WebService 客户端调用 代码:在项目中从非项目内的接口获取数 ...
- MongoDB同步机制
复制 在此页 冗余和数据可用性 在MongoDB中复制 异步复制 自动故障转移 读取操作 交易次数 更改流 附加的功能 甲副本集 MongoDB中是一组mongod其保持相同的数据集的过程.副本集提供 ...
- Java并发包--线程池原理
转载请注明出处:http://www.cnblogs.com/skywang12345/p/3509954.html 线程池示例 在分析线程池之前,先看一个简单的线程池示例. 1 import jav ...
- 快速搭建FTP服务器
快速搭建一个本地的FTP服务器 如果需要开发FTP文件上传下载功能,那么需要在本机上搭建一个本地FTP服务器,方便调试.第一步:配置IIS Web服务器1.1 控制面板中找到“程序”并打开 1.2 ...
- list 对像排序
在C#的List操作中,针对List对象集合的排序我们可以使用OrderBy.OrderByDescending.ThenBy.ThenByDescending等方法按照特定的对象属性进行排序,其中O ...
- Java8-Lambda-No.04
public class Lambda4 { static int outerStaticNum; int outerNum; void testScopes() { int num = 1; Lam ...
- 2019牛客多校第四场A meeting——树的直径
题意: 一颗 $n$ 个节点的树上标有 $k$ 个点,找一点使得到 $k$ 个关键结点的最大距离最小. 分析: 问题等价于求树的直径,最小距离即为直径除2向上取整. 有两种求法,一是动态规划,对于每个 ...
- ajax 页面无刷新
<!-- 使用原生Ajax 和 $.ajax 实现局部刷新的过程 --><!-- 封装通用XMLHttpRequest对象 --><!DOCTYPE html>&l ...