当前位置:网站首页>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 <= 30class 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
边栏推荐
- Blocking of concurrency control
- AD637使用笔记
- How to add new fields to mongodb with code (all)
- Blocking protocol for concurrency control
- Decorator learning 01
- The new content of the text component can be added through the tag_ Config set foreground and background colors
- The real situation of programmers
- C language knowledge points link
- Calculation method of boundary IOU
- Livelocks and deadlocks of concurrency control
猜你喜欢

MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server

Blocking of concurrency control

Server optimization of performance tuning methodology

Interview questions for famous enterprises: Coins represent a given value

【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用

Decorator learning 01

Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)

Dbeaver executes multiple insert into error processing at the same time

Talking about MySQL index

"Chris Richardson microservices series" uses API gateway to build microservices
随机推荐
Performance monitoring of database tuning solutions
Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
Interprocess communication in the "Chris Richardson microservice series" microservice architecture
A trip to Suzhou during the Dragon Boat Festival holiday
Oracle hint understanding
Tips for using SecureCRT
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
K210 learning notes (IV) k210 runs multiple models at the same time
Oracle advanced query
Alternating merging strings of leetcode simple questions
Database recovery strategy
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
AD637 usage notes
boundary IoU 的计算方式
Serializability of concurrent scheduling
MySQL服务莫名宕机的解决方案
How can Bluetooth in notebook computer be used to connect headphones
Microservice link risk analysis
Analyse des risques liés aux liaisons de microservices
Some tutorials install the database on ubantu so as not to occupy computer memory?