当前位置:网站首页>509. Fibonacci Number. Sol
509. Fibonacci Number. Sol
2022-07-05 22:17:00 【isee_ nh】
still Easy The subject of , So do more to improve self-confidence (bushi)
Recursion is simple , But this is not done with recursion , Is to use ordinary iteration , Just one for Circulation is enough
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: n = 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
Example 2:
Input: n = 3 Output: 2 Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
Example 3:
Input: n = 4 Output: 3 Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
Constraints:
0 <= n <= 30
class Solution: def fib(self, n): if n==0 or n==1: return n else: prev1 = self.fib(0) prev2 = self.fib(1) for _ in range(n-1): temp = prev2 prev2 = prev1 + prev2 prev1 = temp return prev2
边栏推荐
- A long's perception
- Microservice link risk analysis
- The difference between MVVM and MVC
- 如何開發引入小程序插件
- Recovery technology with checkpoints
- MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
- Tips for using SecureCRT
- 如何快速体验OneOS
- 2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
- The American Championship is about to start. Are you ready?
猜你喜欢
AD637使用笔记
MySQL服务莫名宕机的解决方案
K210学习笔记(四) K210同时运行多个模型
2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
Official clarification statement of Jihu company
Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
A substring with a length of three and different characters in the leetcode simple question
ESP32 hosted
EBS Oracle 11g cloning steps (single node)
Oracle advanced query
随机推荐
Bitbucket installation configuration
Hysbz 2243 staining (tree chain splitting)
The real situation of programmers
How to develop and introduce applet plug-ins
Multiplexing of Oracle control files
854. String BFS with similarity K
Win11运行cmd提示“请求的操作需要提升”的解决方法
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
如何快速体验OneOS
2022-07-05:给定一个数组,想随时查询任何范围上的最大值。 如果只是根据初始数组建立、并且以后没有修改, 那么RMQ方法比线段树方法好实现,时间复杂度O(N*logN),额外空间复杂度O(N*
IIC bus realizes client device
HDU 4391 paint the wall segment tree (water
装饰器学习01
Meituan dynamic thread pool practice ideas, open source
MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
Oracle views the data size of a table
Web3为互联网带来了哪些改变?
Common interview questions of JVM manufacturers
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)