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. redash oracle 数据源docker 镜像

    redash 官方的docker 镜像是没有包含oracle的,需要我们自己添加,参考了一个docker 镜像进行了简单的修改 Dockerfile FROM redash/redash:7.0.0. ...

  2. terraform v0.12.0 发布了

    v0.12.0 相比以前的有好多新的特性,包括语法,以及函数增强,昨天还在折腾的一个json解码的问题,直接使用 v0.12.0 就可以解决了,同时也包含了for 操作处理同时官方文档对于v0.12. ...

  3. [转]查看 docker 容器使用的资源

    作者:sparkdev 出处:http://www.cnblogs.com/sparkdev/     在容器的使用过程中,如果能及时的掌握容器使用的系统资源,无论对开发还是运维工作都是非常有益的.幸 ...

  4. 个人Vim配置(即vim目录下vimrc_)

    因为是C++选手所以大部分带有Dev遗留的...格式 colorscheme molokai"配色方案,注意molokai不是自带而是自己调配的,SublimeText3标准配色,想要的点这 ...

  5. 什么是uni-app?

    uni-app 是一个使用 Vue.js 开发所有前端应用的框架,开发者编写一套代码,可发布到iOS.Android.H5.以及各种小程序(微信/支付宝/百度/头条/QQ/钉钉)等多个平台. 即使不跨 ...

  6. <英狼>--团队作业3 王者光耀--终极版

    队员 陶俊宇_031702113 卞永亨_031702229 唐怡_031702109 Github 吉哈---King-Shines 队员输出百分比,数据为估值仅供参考 MVP:队长:陶俊宇 60% ...

  7. kms windows激活

    Microsoft KMS Activation Usage Start a Command Prompt as an Administrator. Windows slmgr.vbs -upk sl ...

  8. spring @Autowired注入map

    注入map,平常一般不会这么做,今天看一段老代码时发现有这么个用法.补习一下. @Autowired 标注作用于 Map 类型时,如果 Map 的 key 为 String 类型,则 Spring 会 ...

  9. 取消本地文件夹与SVN服务器的关联

    我们在开发项目中用SVN作为版本管理工具时,从服务器下载到本地的项目是有.svn文件夹的,这个代表是与svn服务器代码相关联的,如果我们想取消本地文件夹与svn服务器的关联,那么有多种方法,这里介绍导 ...

  10. Spring 整合 myBatis

    思路 数据库连接池交给 Spring 管理 SqlSessionFactory 交给 Spring 管理 从 Spring 容器中直接获得 mapper 的代理对象 步骤 创建工程 导入 jar 创建 ...