当前位置:网站首页>[dynamic planning] 70. Climbing stairs
[dynamic planning] 70. Climbing stairs
2022-07-25 11:24:00 【fatfatmomo】
Suppose you're climbing the stairs . need n You can reach the top of the building .
Every time you climb 1 or 2 A stair . How many different ways can you climb to the top of the building ?
Be careful : Given n Is a positive integer .
Beyond the time limit method ;
class Solution {
public:
int climbStairs(int n) {
if(n==1)
{
return 1;
}
if(n==2)
{
return 2;
}
return climbStairs(n-1)+climbStairs(n-2);
}
};
At the beginning with vector Store array , The result is still a long time , Switch to f1,f2 了 :
class Solution {
public:
int climbStairs(int n) {
int i,f1,f2,sum;
if(n==1)
{
return 1;
}
if(n==2)
{
return 2;
}
f1=1;
f2=2;
for(i=3;i<=n;i++)
{
sum=f1+f2;
f1=f2;
f2=sum;
}
return f2;
}
};
There is still room for optimization in time , Only more than 39.43% user
边栏推荐
- How can you use unity without several plug-ins? Unity various plug-ins and tutorial recommendations
- [recursion] 938. Range and of binary search tree
- My colleague looked at my code and exclaimed: how can I use a singleton in unity
- HCIP(13)
- Code representation learning: introduction to codebert and other related models
- The University of Gottingen proposed clipseg: a model that can perform three segmentation tasks simultaneously using text and image prompts
- Understand the life cycle and route jump of small programs
- HCIA experiment (06)
- Guys, flick CDC table API, Mysql to MySQL, an application that can
- UE4.26源码版学习广域网独立服务器时遇到的客户端运行黑屏问题
猜你喜欢

NowCoderTOP1-6——持续更新ing

从宏观到微观 零基础 详解bert

Reinforcement learning (III)

Implementation of recommendation system collaborative filtering in spark

Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots

Hcip experiment (03)

HCIA experiment (07) comprehensive experiment

MySQL advanced statement (I) (there is always someone who will make your life no longer bad)

Esp8266 uses drv8833 drive board to drive N20 motor

HCIP (01)
随机推荐
新能源销冠宏光MINIEV,有着怎样的产品力?
[information system project manager] thought map series essence summary
SQL语言(二)
Signal integrity (SI) power integrity (PI) learning notes (XXXIV) 100 rules of thumb for estimating signal integrity effects
100W!
C# Newtonsoft.Json 高级用法
Mlx90640 infrared thermal imager temperature measurement module development notes (V)
Last week's hot review (7.18-7.24)
Detailed explanation of the implementation method of DNS separation and resolution
Stm32cubemx learning record -- installation, configuration and use
B2B2C多商户系统功能丰富,极易二开!!!
[递归] 938. 二叉搜索树的范围和
[domain generalization] 2022 IJCAI domain generalization tutorial Report
JS convert pseudo array to array
HCIA experiment (08)
Learn PHP -- phpstudy tips mysqld Exe: Error While Setting Value ‘NO_ ENGINE_ Solution of substitution error
Code representation learning: introduction to codebert and other related models
Motivation of enterprises to practice open source
mysql主从复制与读写分离
Learn Luzhi PHP -- tp5.0 uses Chinese as an alias and reports "unsupported data expression"