problem

509. Fibonacci Number

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的更多相关文章

  1. 【LeetCode】509. Fibonacci Number 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  2. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  3. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  4. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  5. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  6. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  7. 【Leetcode】179. Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  8. 【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 ...

  9. 【LeetCode】509. 斐波那契数

    题目 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列.该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和.也就是: F(0) = 0,   F(1) = 1 F(N) = ...

随机推荐

  1. Python window console 控制台 实现最后一行输出 print 重写

    Python window console 控制台 实现最后一行输出 print 重写 # -*- coding: utf-8-*- from __future__ import print_func ...

  2. 拒绝流量劫持,全面使用 HTTPS!

    最近收到数个 BootCDN 用户的反馈:某些地区的宽带运营商劫持了部分 BootCDN 上的文件,并篡改文件加入了广告代码. 这种方式的流量劫持属于中间人攻击(Man-in-the-Middle A ...

  3. ActiveMQ之JMS及保证消息的可靠性<持久化、事务、签收>(三)

    1.JAVAEE 是一套使用Java 进行企业级开发的13 个核心规范工业标准 , 包括: JDBC  数据库连接 JNDI  Java的命名和目录接口 EJB   Enterprise java b ...

  4. spring配置和映射文件

    配置 <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www. ...

  5. 使用jdk自带的线程池。加载10个线程。

    在开发中使用线程,经常不经意间就new Thread()一个出来,然后发现,这样做不是很好,特别是很多线程同时处理的时候,会出现CPU被用光导致机器假死,线程运行完成自动销毁后,又复活的情况. 所以在 ...

  6. redis在linux服务器部署

    0)参考资料 http://www.cnblogs.com/liuling/p/2014-4-19-02.html 1)下载安装包地址 http://download.redis.io/release ...

  7. mapbox-gl空间分析插件turf.js使用介绍

    mapbox-gl能够方便地显示地图,做一些交互,但是缺少空间分析功能,比如绘制缓冲区.判断点和面相交等等. turf.js是一个丰富的用于浏览器和node.js空间分析库,官网 http://tur ...

  8. 10、Spring Boot 2.x 集成 Log4j2

    1.10 Spring Boot 2.x 集成 Log4j2 完整源码: Spring-Boot-Demos

  9. Appium自动化测试教程-自学网-monkey参数

    monkey 参数 参数分类 · 常规类参数 · 事件类参数 · 约束类参数 · 调试类参数 常规类参数 常规类参数包括帮助参数和日志信息参数.帮助参数用于输出Monkey命令使用指导:日志信息参数将 ...

  10. P4053 [JSOI2007]建筑抢修 堆贪心

    思路:堆贪心 提交:1次 题解: 先按时间\(sort\),然后如果能修就直接扔堆里,不能修取堆顶比一下时间长短,把时间短的扔进堆: #include<cstdio> #include&l ...