当前位置:网站首页>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
边栏推荐
- Business learning of mall order module
- database mirroring
- Interprocess communication in the "Chris Richardson microservice series" microservice architecture
- 如何開發引入小程序插件
- Platform bus
- The simple problem of leetcode is to split a string into several groups of length K
- Granularity of blocking of concurrency control
- Character conversion PTA
- Shell script, awk condition judgment and logic comparison &||
- Leetcode simple question: check whether each row and column contain all integers
猜你喜欢
Talking about MySQL index
The simple problem of leetcode is to split a string into several groups of length K
Server optimization of performance tuning methodology
Damn, window in ie open()
科技云报道:算力网络,还需跨越几道坎?
Nacos 的安装与服务的注册
Ad637 notes d'utilisation
MySQL actual combat 45 lecture learning (I)
Reptile practice
MySQL服务莫名宕机的解决方案
随机推荐
每日刷题记录 (十四)
Analyse des risques liés aux liaisons de microservices
Matlab draws a cute fat doll
Practice: fabric user certificate revocation operation process
When the industrial Internet era is truly mature, we will look at the emergence of a series of new industrial giants
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
FBO and RBO disappeared in webgpu
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
Concurrency control of performance tuning methodology
Serializability of concurrent scheduling
Metaverse Ape获Negentropy Capital种子轮融资350万美元
Oracle hint understanding
[Yugong series] go teaching course in July 2022 004 go code Notes
What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
ESP32 hosted
元宇宙中的三大“派系”
How to organize an actual attack and defense drill
Shell script, awk uses if, for process control
Technology cloud report won the special contribution award for the 10th anniversary of 2013-2022 of the "cloud Ding Award" of the global cloud computing conference