当前位置:网站首页>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
边栏推荐
- 1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
- Hysbz 2243 staining (tree chain splitting)
- Type of fault
- Unique occurrence times of leetcode simple questions
- A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
- 如何快速体验OneOS
- Oracle triggers
- Poj3414 extensive search
- 数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
- Decorator learning 01
猜你喜欢

元宇宙中的三大“派系”

A substring with a length of three and different characters in the leetcode simple question

Metaverse Ape上线倒计时,推荐活动火爆进行

AD637使用笔记

Index optimization of performance tuning methodology

Cobaltstrike builds an intranet tunnel

"Chris Richardson microservices series" uses API gateway to build microservices

EBS Oracle 11g cloning steps (single node)

The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!

Oracle hint understanding
随机推荐
Damn, window in ie open()
ESP32 hosted
Leetcode simple question: the minimum cost of buying candy at a discount
How to quickly experience oneos
IIC bus realizes client device
A long's perception
How to develop and introduce applet plug-ins
Leetcode simple question check whether all characters appear the same number of times
Solutions for unexplained downtime of MySQL services
Matlab draws a cute fat doll
Leetcode simple question: check whether each row and column contain all integers
2022-07-05:给定一个数组,想随时查询任何范围上的最大值。 如果只是根据初始数组建立、并且以后没有修改, 那么RMQ方法比线段树方法好实现,时间复杂度O(N*logN),额外空间复杂度O(N*
Pl/sql basic case
微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
Overview of database recovery
Character conversion PTA
Database recovery strategy
Oracle hint understanding
CRM creates its own custom report based on fetch
Interprocess communication in the "Chris Richardson microservice series" microservice architecture