当前位置:网站首页>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
边栏推荐
- Concurrency control of performance tuning methodology
- What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
- Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
- Overview of concurrency control
- Metaverse Ape上线倒计时,推荐活动火爆进行
- ESP32 hosted
- Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
- 从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
- Search: Future Vision (moving sword)
- Alternating merging strings of leetcode simple questions
猜你喜欢
Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
数据泄露怎么办?'华生·K'7招消灭安全威胁
Pl/sql basic syntax
Ad637 notes d'utilisation
How can Bluetooth in notebook computer be used to connect headphones
Recovery technology with checkpoints
Three "factions" in the metauniverse
极狐公司官方澄清声明
"Chris Richardson microservices series" uses API gateway to build microservices
Oracle hint understanding
随机推荐
HDU 4391 paint the wall segment tree (water
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
Hcip day 16
Meituan dynamic thread pool practice ideas, open source
Analyse des risques liés aux liaisons de microservices
Oracle views the data size of a table
Metaverse Ape上线倒计时,推荐活动火爆进行
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
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
Livelocks and deadlocks of concurrency control
Oracle is sorted by creation time. If the creation time is empty, the record is placed last
Alternating merging strings of leetcode simple questions
Common interview questions of redis factory
Server optimization of performance tuning methodology
Stored procedures and stored functions
Cobaltstrike builds an intranet tunnel
Reptile practice
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
如何開發引入小程序插件
database mirroring