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. SVN 常用 还原项目

    1.先修改本来两个文件,然后再提交到SVN 2.在日志界面,查看提交的文件,找到对应的版本号 3.找到对应的版本号(这里的版本号是1995,我提交生成的版本号 的前一个版本 才是我未作出修改的版本), ...

  2. 【JZOJ6227】【20190621】ichi

    题目 $n , m ,d,x\le 10^5 , $强制在线 题解 对原树做dfs,得到原树的dfs序 对kruksal重构树做dfs,得到重构树的dfs序 那么就是一个三维数点问题 强制在线并且卡空 ...

  3. 65、Spark Streaming:数据接收原理剖析与源码分析

    一.数据接收原理 二.源码分析 入口包org.apache.spark.streaming.receiver下ReceiverSupervisorImpl类的onStart()方法 ### overr ...

  4. PHP 打印输出数组内容及结构 print_r 与 var_dump 函数

    利用 print_r() 函数可以打印输出整个数组内容及结构,按照一定格式显示键和元素.注意 print_r() 函数不仅是只用于打印,实际它是用于打印关于变量的易于理解的信息. 例子1 <?p ...

  5. EasyEarth三维可视化解决方案——智慧林业

    智慧林业 智能巡管监护 护林员信息查询 护林员管护范围查询 护林员报警.采集数据查看 样点.样线管理 其它功能模块 ●一键考勤打卡 ●面积测量 ●任务公告发布 ●实时电量监控 ●一键报警功能 ●北斗短 ...

  6. dedecms 模板文件不存在,无法解析文档的终极各种解决办法

    dedecms 模板文件不存在,无法解析文档"的终极各种解决办法 方法一:[此对应喜欢把模板文件使用".html"的格式,] /include/arc.archives. ...

  7. Intellij IDEA 自动清除无效 import 和 清除无效 import 的快捷键

    可以settings-general-auto import-java项,勾选optimize imports on the fly,在当前项目下会自动清除无效的import,而且这个是随时自动清除的 ...

  8. Edusoho之Basic Authentication

    通过如下代码,可以正常请求并获取对应的数据: curl -X POST -H "Accept:application/vnd.edusoho.v2+json" -H "A ...

  9. 刷题记录:[FBCTF2019]Products Manager

    目录 刷题记录:[FBCTF2019]Products Manager 一.知识点 1.基于约束的SQL注入攻击 刷题记录:[FBCTF2019]Products Manager 题目复现链接:htt ...

  10. 京津冀大学生竞赛:babyphp

    京津冀大学生竞赛:babyphp 比赛的时候没做出来,回来以后一会就做出来了,难受...还是基本功不扎实,都不记得__invoke怎么触发的了 放上源码 <?php error_reportin ...