题目地址:Fibonacci Number - LeetCode


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.
Given N, calculate F(N).

Example 1:

Input: 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2:

Input: 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.

Example 3:

Input: 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.

Note:

  • 0 ≤ N ≤ 30.

最经典的斐波那契数列,有多种解法。

标准解法如下:

class Solution:def fib(self, N: int) -> int:if N == 0 or N == 1:return Na, b = 0, 1for i in range(1, N):a, b = b, a+breturn b

当然,如果你直接套公式,速度自然更快。

其他解法也不写了。

LeetCode 509. Fibonacci Number--Python解法相关推荐

  1. Leetcode 509. Fibonacci Number

    class Solution(object):def fib(self, N):""":type N: int:rtype: int"""F ...

  2. 【Leetcode】5 longestPalindrome python解法

    leetcode第5题 longestPalindrome 最长回文子串 python解法 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 输入: " ...

  3. LeetCode 15. 3Sum--Java,Python解法

    题目地址: Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? ...

  4. LeetCode刷题之python解法(持续更新)

    1. Two Sum 4行 class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:d = {}fo ...

  5. 每日一道Leetcode算法——Fibonacci Number——2019.02.01

    题干: 中文: Fibonacci数字,通常表示为F(n),形成一个称为Fibonacci序列的序列, 这样每个数字是前两个数字的总和,从0和1开始. 即, F(0)= 0,F(1)= 1 对于N&g ...

  6. Java/509. Fibonacci Number 斐波那契数

    题目 代码部分(0ms 100%) class Solution {public int fib(int N) {if(N == 0) return 0;if(N == 1) return 1;int ...

  7. LeetCode 动态规划(Dynamic programming)系列题目--C++,Python解法

    LeetCode上有许多态规划(Dynamic programming)的题目,我在这里整合一下 本文章不再更新,请看LeetCode 所有题目总结 LeetCode 所有题目总结:LeetCode ...

  8. LeetCode 264. Ugly Number II--C++,Python解法

    题目地址:Ugly Number II - LeetCode Write a program to find the n-th ugly number. Ugly numbers are positi ...

  9. LeetCode 137. Single Number II--三次异或消除相同的数--C++,Python解法

    题目地址:Single Number II - LeetCode Given a non-empty array of integers, every element appears three ti ...

最新文章

  1. python怎么保存数据框转置结果_python – 如何转置pandas数据帧以交叉制表保存所有值的数据帧...
  2. [记录]使用openGL显示点云的一个程序
  3. GC DevKit 快速入门 -- 游戏概览(三)
  4. [通告]Nuget服务宕机,出现 503 Server Unavailable 错误无法编译及解决方法
  5. php mysql 博客制作_PHP实现简易blog的制作
  6. Winform中使用FastReport实现自定义PDF打印预览
  7. logic.c:1:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘’ token...
  8. java自动定义一个map类_自定义实现Map类 高效版
  9. 来来来,送开发板了。
  10. java短信验证码功能发送的验证码如何校验_企业如何选择短信平台才能保障安全和稳定性?...
  11. app测试的过程和重点关注内容
  12. 昔年浅谈化工平台网站怎么吸引客户咨询入驻呢?
  13. kettle数据脱敏
  14. Windows的文件目录管理策略
  15. IOI2020国家集训队作业乱做
  16. 现在买笔记本电脑要注意哪些方面? 哪些比较好?
  17. C++ 后室·无限#1 游戏制作实录(RPG类型游戏)
  18. ionic的生命周期
  19. 2021河南普通话考试的时间和内容
  20. F#match的强大和List.tail方法的真实含义

热门文章

  1. RDKit | 基于分子指纹的分子相似性
  2. RDKit | 可视化重要片段
  3. RDKit:化合物亚结构(Substructure)搜索(基于Python3)
  4. GEB:中科院南土所梁玉婷组-大陆尺度下稻田土壤微生物β多样性的纬度分布特征...
  5. 高级转录组分析和R数据可视化专题研讨会(2019.12)
  6. 2019微生物组—宏基因组分析技术研讨会第五期
  7. Cytoscape制作带bar图和pie图节点的网络图
  8. 如何第一时间了解研究领域最新动态?
  9. 我们从那里来—NG子宫日记 Womb
  10. R语言多因素方差分析及评估假设检验