博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】509. Fibonacci Number
阅读量:6946 次
发布时间:2019-06-27

本文共 819 字,大约阅读时间需要 2 分钟。

题目如下:

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) = 1F(N) = F(N - 1) + F(N - 2), for N > 1.

Given N, calculate F(N).

 

Example 1:

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

Example 2:

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

Example 3:

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

 

Note:

0 ≤ N ≤ 30.

解题思路:递归的经典例子,当然为了提高效率,可以缓存中间结果。这题好像以前是Premium的,最近解锁了。

代码如下:

class Solution(object):    def fib(self, N):        """        :type N: int        :rtype: int        """        if N == 0 or N == 1:            return N        return self.fib(N-1) + self.fib(N-2)

 

转载于:https://www.cnblogs.com/seyjs/p/10383202.html

你可能感兴趣的文章
android ScrollView中嵌套listview listview可点击处理,可展开
查看>>
一个由proguard与fastJson引起的血案(转)
查看>>
clearing & settlement
查看>>
Lingo 做线性规划 - DEA
查看>>
Axure 全局辅助线(转)
查看>>
js原生设计模式——9外观模式封装2(小型代码库YJ)
查看>>
onvif开发实战2--总结框架搭建
查看>>
Apache ab测试工具使用方法(无参、get传参、post传参)
查看>>
单例设计模式之安全的懒汉式
查看>>
iOS_20_微博OAuth授权_取得用户授权的accessToken
查看>>
离线用户的灰色头像处理
查看>>
php递归函数return会出现无法正确返回想要值的情况
查看>>
Android Studio之Activity切换动画(三)
查看>>
Bitcoin: A Peer-to-Peer Electronic Cash System(比特币论文翻译)
查看>>
Redis-Redi事务注意事项
查看>>
ffmpeg mediacodec 硬解初探
查看>>
Cocostudio 1.4 实现的DemoShop
查看>>
Ambari-Blueprint介绍
查看>>
可编辑ztree节点的增删改功能图标控制---已解决
查看>>
Android-自己定义标题栏
查看>>