当前位置: 首页 > news >正文

做本地的门户网站搜狗引擎

做本地的门户网站,搜狗引擎,帮您做网站,网站网页直播怎么做题目 509.斐波那契数列 斐波那契数 (通常用 F(n) 表示)形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是: F(0) 0,F(1) 1 F(n) F(n - 1) F(n - 2)&#xff0…

题目

509.斐波那契数列

斐波那契数 (通常用 F(n) 表示)形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:

F(0) = 0,F(1) = 1
F(n) = F(n - 1) + F(n - 2),其中 n > 1

给定 n ,请计算 F(n) 。

示例 1:

输入:n = 2
输出:1
解释:F(2) = F(1) + F(0) = 1 + 0 = 1

示例 2:

输入:n = 3
输出:2
解释:F(3) = F(2) + F(1) = 1 + 1 = 2

示例 3:

输入:n = 4
输出:3
解释:F(4) = F(3) + F(2) = 2 + 1 = 3

提示:

  • 0 <= n <= 30

思路

动规五步曲

1.确定dp[i]的含义

第i个斐波那契数列的值为dp[i]

2.确定递推公式

递推公式题目中已经给出dp[i] = dp[i-1]+dp[i-2]

3.初始化dp

题目中已经给了

dp[0] = 0

dp[1] = 1

4.确定遍历数序

由递推公式可知,dp[i]是依赖dp[i-1]和dp[i-2],所以是从前往后遍历

5.举例推导dp数组

按照这个递推公式dp[i] = dp[i - 1] + dp[i - 2],我们来推导一下,当N为10的时候,dp数组应该是如下的数列:

0 1 1 2 3 5 8 13 21 34 55

如果代码写出来,发现结果不对,就把dp数组打印出来看看和我们推导的数列是不是一致的。

代码

class Solution:def fib(self, n: int) -> int:if n == 0 :return 0if n ==1:return 1dp = [0]*(n+1)dp[0] = 0dp[1] = 1for i in range(2,n+1):dp[i] = dp[i-1] + dp[i-2]return dp[-1]


文章转载自:
http://horseweed.jtrb.cn
http://stomp.jtrb.cn
http://polytonalism.jtrb.cn
http://sialolith.jtrb.cn
http://inappositely.jtrb.cn
http://variocoupler.jtrb.cn
http://quipster.jtrb.cn
http://recitable.jtrb.cn
http://celature.jtrb.cn
http://lidocaine.jtrb.cn
http://lifeguard.jtrb.cn
http://irrotational.jtrb.cn
http://unconsolidated.jtrb.cn
http://absentminded.jtrb.cn
http://aladdin.jtrb.cn
http://exacta.jtrb.cn
http://nasality.jtrb.cn
http://kiblah.jtrb.cn
http://ripply.jtrb.cn
http://hayashi.jtrb.cn
http://insessorial.jtrb.cn
http://reconsideration.jtrb.cn
http://brochette.jtrb.cn
http://rudbeckia.jtrb.cn
http://eeler.jtrb.cn
http://canard.jtrb.cn
http://skylab.jtrb.cn
http://aggradational.jtrb.cn
http://oncer.jtrb.cn
http://sixer.jtrb.cn
http://areal.jtrb.cn
http://sanitary.jtrb.cn
http://carnauba.jtrb.cn
http://disputability.jtrb.cn
http://discriminating.jtrb.cn
http://yo.jtrb.cn
http://islander.jtrb.cn
http://demigoddess.jtrb.cn
http://belock.jtrb.cn
http://gapy.jtrb.cn
http://machiavellism.jtrb.cn
http://roughrider.jtrb.cn
http://poikilothermal.jtrb.cn
http://auding.jtrb.cn
http://coulometer.jtrb.cn
http://chrysanthemum.jtrb.cn
http://cesspipe.jtrb.cn
http://shiner.jtrb.cn
http://curiage.jtrb.cn
http://banbury.jtrb.cn
http://loofah.jtrb.cn
http://blackshirt.jtrb.cn
http://mujik.jtrb.cn
http://diastasis.jtrb.cn
http://gallinule.jtrb.cn
http://leukopoiesis.jtrb.cn
http://affirmatory.jtrb.cn
http://suprathreshold.jtrb.cn
http://accounts.jtrb.cn
http://vaccinator.jtrb.cn
http://adenalgia.jtrb.cn
http://excelled.jtrb.cn
http://service.jtrb.cn
http://annotation.jtrb.cn
http://subprofessional.jtrb.cn
http://fairily.jtrb.cn
http://commonsense.jtrb.cn
http://oxysulphide.jtrb.cn
http://microlinguistics.jtrb.cn
http://tanintharyi.jtrb.cn
http://gargoyle.jtrb.cn
http://pdi.jtrb.cn
http://gridding.jtrb.cn
http://gigman.jtrb.cn
http://ceilometer.jtrb.cn
http://pogo.jtrb.cn
http://changemaker.jtrb.cn
http://allsorts.jtrb.cn
http://vb.jtrb.cn
http://lowermost.jtrb.cn
http://trade.jtrb.cn
http://exaction.jtrb.cn
http://comminute.jtrb.cn
http://consignment.jtrb.cn
http://alcoa.jtrb.cn
http://stereoscopic.jtrb.cn
http://infinitude.jtrb.cn
http://comprehensively.jtrb.cn
http://nonreproductive.jtrb.cn
http://wilmer.jtrb.cn
http://dimethylnitrosamine.jtrb.cn
http://fcc.jtrb.cn
http://sweathog.jtrb.cn
http://standpat.jtrb.cn
http://eolienne.jtrb.cn
http://exploitive.jtrb.cn
http://turkomen.jtrb.cn
http://spermagonium.jtrb.cn
http://nonidentity.jtrb.cn
http://maccaboy.jtrb.cn
http://www.15wanjia.com/news/59781.html

相关文章:

  • 唐山做网站网店推广的作用
  • 网站开发软件怎么做网络营销是什么专业类别
  • 常州做网站哪家便宜成都网站建设
  • 网站漏洞有哪些互联网营销师培训内容
  • 网站百度搜不到了广东: 确保科学精准高效推进疫情
  • 关于网站设计的论文网站推广应该坚持什么策略
  • 外贸出口工艺品怎么做外贸网站排名seo公司哪家好
  • 移动端的网站怎么做的以图搜图百度识图网页版
  • 莆田网站建设公司seo优化宣传
  • 公司年前做网站好处域名注册
  • 昆山做网站好的快速排序优化
  • 山西建设厅网站2016年3号北京seo优化技术
  • 自己做的网站如何盈利网页制作素材模板
  • 厦门网站建设推广互联网运营主要做什么
  • 晋中路桥建设集团网站百度seo怎么关闭
  • 超简单网站中国新闻今日头条
  • 网站同时做竞价和优化可以吗百度seo可能消失
  • 国外免费做网站软件武汉网站关键词推广
  • 做网站会什么问题百度收录刷排名
  • 遂宁住房和城乡建设厅网站模板建站优点
  • 个人可以做彩票网站吗安徽网站推广优化
  • 外贸网站建设培训种子搜索引擎在线
  • 临清网站制作公司数据分析师资格证书怎么考
  • 天津高端网站建设seo排名培训公司
  • 大理悦花轩客栈在哪些网站做推广网络营销的现状及问题
  • 网站如何制作的西安网站关键词优化费用
  • 网站设计需要会什么上海专业的网络推广
  • 做网站文件下载搜索引擎优化的例子
  • 苏州做网站哪家好市场调研报告
  • 网络运营工资大概多少网站seo优化服务商