当前位置:网站首页>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
边栏推荐
- Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
- Common interview questions of JVM manufacturers
- C language knowledge points link
- The simple problem of leetcode is to split a string into several groups of length K
- 【愚公系列】2022年7月 Go教学课程 004-Go代码注释
- Shell script, awk condition judgment and logic comparison &||
- Meituan dynamic thread pool practice ideas, open source
- Reptile practice
- The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
- PyGame practical project: write Snake games with 300 lines of code
猜你喜欢
The statistics of leetcode simple question is the public string that has appeared once
A substring with a length of three and different characters in the leetcode simple question
EBS Oracle 11g cloning steps (single node)
Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
Cobaltstrike builds an intranet tunnel
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
Advantages and disadvantages of the "Chris Richardson microservice series" microservice architecture
Daily question brushing record (XIV)
Stored procedures and stored functions
每日刷题记录 (十四)
随机推荐
How can Bluetooth in notebook computer be used to connect headphones
笔记本电脑蓝牙怎么用来连接耳机
Text组件新增内容通过tag_config设置前景色、背景色
了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
Technology cloud report: how many hurdles does the computing power network need to cross?
极狐公司官方澄清声明
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
A substring with a length of three and different characters in the leetcode simple question
Index optimization of performance tuning methodology
科技云报道:算力网络,还需跨越几道坎?
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
Dbeaver executes multiple insert into error processing at the same time
How to develop and introduce applet plug-ins
Oracle is sorted by creation time. If the creation time is empty, the record is placed last
Create a virtual machine on VMware (system not installed)
Oracle views the data size of a table
MySQL actual combat 45 lecture learning (I)
DataGrid directly edits and saves "design defects"
How to view Apache log4j 2 remote code execution vulnerability?
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用