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. SQL基础-建表

    一.建表 1.创建表的两种方式 *客户端工具 *SQL语句 2.使用SQL语句创建表 表名和字段名不能使用中文:(一般为字母开头,字母.数字.下划线组成的字符串): CREATE TABLE关键字后跟 ...

  2. HTML盒子模型冷知识!!!

    一.边框    1.边框颜色     border-color 边框颜色(可以设置上边框,如:border-top-color,也可以整体设置,但是要注意顺序)     2.边框粗细     bord ...

  3. Cannot read property '_withTask' of undefined

    前言 Cannot read property '_withTask' of undefined 突然一下子,就报这个错了,刚刚还好好呢 Bug分析 1.是在template中调用了某个方法,但是你没 ...

  4. C++之宏、extern关键字与多线程

    理解C++ 宏 1.特殊字符 考虑下面的需求,程序中多处使用文本字符串.我们知道文本字符串前后都要加上双引号,我很讨厌输入双引号.有没有好的办法呢?根据常识,使用下面的宏: #define Str(x ...

  5. Python3爬取王者官方网站英雄数据

    爬取王者官方网站英雄数据 众所周知,王者荣耀已经成为众多人们喜爱的一款休闲娱乐手游,今天就利用python3 爬虫技术爬取官方网站上的几十个英雄的资料,包括官方给出的人物定位,英雄名称,技能名称,CD ...

  6. 团队作业-Beta版本演示

    组长博客链接 https://www.cnblogs.com/cmlei/p/12063671.html 本组成员 031702431 陈明磊 组长 031702227 林镕炜 031702413 韩 ...

  7. [转]OpenGL图形渲染管线、VBO、VAO、EBO概念及用例

    直接给出原文链接吧 1.OpenGL图形渲染管线.VBO.VAO.EBO概念及用例 2.OpenGL中glVertex.显示列表(glCallList).顶点数组(Vertex array).VBO及 ...

  8. springMVC中controller的传参的几种案例

    1.springmvc的controller方法不指定method时,默认get/post都支持 //@RequestMapping(value="test") //@Reques ...

  9. [python语法]python中如何判断一个集合是另一个集合的子集?

    问:python中如何判断一个集合是另一个集合的子集? 答:用issubset()方法 语法: A.issubset(B) 返回: True 如果A是B的子集. False 如果A不是B的子集. 样例 ...

  10. win10找不到hosts文件(设置显示隐藏文件也找不到)解决方法

    Win10系统中的Hosts文件有很多作用,屏蔽网址,指定解析,跳转等等,所以我们经常会通过编辑Hosts文件来达成一些目的,一般来说hosts文件是隐藏的,我们需要显示受保护的文件才可以,但是有一些 ...