package y2019.Algorithm.array;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: Fib
* @Author: xiaof
* @Description: 509. Fibonacci Number
* The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence,
* such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
*
* F(0) = 0, F(1) = 1
* F(N) = F(N - 1) + F(N - 2), for N > 1.
*
* Input: 2
* Output: 1
* Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
*
* @Date: 2019/7/4 9:01
* @Version: 1.0
*/
public class Fib { public int solution(int N) { //因为N每次只需要获取上一个,和上上个的数据,dp一维数组
int pre1 = 0;
int pre2 = 1;
if(N == 0) {
return 0;
} else if (N == 1) {
return pre2;
} else if (N == 2) {
return pre2;
} pre1 = pre2 = 1;
for(int i = 3; i <= N; ++i) {
int temp = pre2;
pre2 += pre1;
pre1 = temp;
} return pre2;
} }

【LEETCODE】44、509. Fibonacci Number的更多相关文章

  1. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  2. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  3. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  4. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  5. 【LeetCode】714、买卖股票的最佳时机含手续费

    Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...

  6. 【leetcode】44. Wildcard Matching

    题目如下: 解题思路:本题和[leetcode]97. Interleaving String非常相似,同样可以采用动态规划的方法.记dp[i][j] = 1或者0 表示pattern[0:i]是否匹 ...

  7. 【LeetCode】287. Find the Duplicate Number

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given an array nums containing n + 1 integer ...

  8. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...

  9. 【LeetCode】2、Add Two Numbers

    题目等级:Medium 题目描述:   You are given two non-empty linked lists representing two non-negative integers. ...

随机推荐

  1. pgloader 学习(九) pg 2 pg 使用with 参数控制同步逻辑

    pgloader 支持比较丰富的配置参数,同时默认数据在同步的时候是会进行索.schema 以及数据的同步对于实际我们可能存在需要进行控制,我们可以通过with 参数方便的处理 参考配置 load 文 ...

  2. zeptojs库

    一.简介 ①Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jquery有着类似的api. ②Zepto的设计目的是提供 jQuery 的类似的API,但并不是100%覆盖 ...

  3. linux命令之------快捷键说明

    linux快捷键说明 (1)命令或目录补齐:Tab (2)遍历历史记录:history 上移:ctrl+p,下移:ctrl+n (3)光标移动 左移:ctrl+b:右移:ctrl+f:移到首部:ctr ...

  4. mysql substring()函数,字符串分割

    mysql> select * from test; +----+------------+-------+-----------+ | id | name | score | subject ...

  5. OpenFOAM——设置自定义非均匀场区域

    在使用OpenFOAM进行计算的时候,我们需要对计算域设置非均匀场,比如最典型的溃坝算例,在开始计算以前,我们需要首先设定某一区域的水的体积分数为1,就是下面这样的: 有可能我们在计算传热问题的时候, ...

  6. SDN阅读作业(二)

    前言碎碎念 当我看到这个全英论文以后,身体和心理都出现了戒断反应,让人无比难受,毕竟自己很久没做过英语阅读理解了.总之,在舍友大佬的帮助下以及各款翻译软件的鼎力支持之下(通篇读完后还是找了中文文献来对 ...

  7. Web前端开发规范之脚本文件和动态文本文件命名规则

    脚本文件:一般使用脚本功能的英文小写缩写命名 实际模块:例如广告条的javascript文件名为ad.js,弹出窗口的javascript文件名为pop.js 公用模块:js文件命名:英文命名,后缀j ...

  8. arcgis python脚本工具实例教程—栅格范围提取至多边形要素类

    arcgis python脚本工具实例教程-栅格范围提取至多边形要素类 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 功能:提取栅格数据的范围, ...

  9. 【maven】插件和依赖管理

    1.插件管理 定义 pluginManagement 用来做插件管理的.它是表示插件声明,即你在项目中的pluginManagement下声明了插件,Maven不会加载该插件,pluginManage ...

  10. Python3基础 list(dict) 使用 * 扩充时,出现字典元素重复问题

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...