Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Pancf
Algorithm
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Algorithm
/
FrogJumpFloor.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
46 lines (43 loc) · 1.44 KB
master
Breadcrumbs
Algorithm
/
FrogJumpFloor.cpp
Copy path
Top
File metadata and controls
Code
Blame
46 lines (43 loc) · 1.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
*题:一只青蛙一次可以跳上一级台阶,也可以跳上两级。求该青蛙跳上一个n级台阶
*总共有多少种跳法?
*假设跳上n级台阶总共有f(n)种跳法,那么最后一步要么是跳一级,要么是跳两级
*也就是说f(n)=f(n-1)+f(n-2),跳上n级台阶的跳法等于跳上n-1级台阶的跳法加上
*跳上n-2级台阶的跳法,由此可得一种递归的解法,而递归的出口就是跳上一级台阶
*只有一种跳法,跳上两级台阶有两种跳法
*/
class solution{
public:
int jumpFloor(int number){
if(number<=0)
return -1;
else if(number==1)
return 1;
else if(number==2)
return 2;
else
return jumpFloor(n-2)+jumpFloor(n-1);
}
}
/*
*当n数值比较大时,递归的做法比较耗时,因此考虑用迭代的办法优化
*/
class solution{
public:
int jumpFloor(int number){
//跳一级或者两级台阶的跳法
if(number==1||number==2)
return number;
int jumpSum=0; //当前台阶的跳法总数
//当前台阶往前退一级的跳法数(初始为两级台阶的跳法数)
int jumpBackStep1=2;
//当前台阶往前退两级的跳法数(初始为一级台阶的跳法数)
int jumpBackStep2=1;
for(int i=3;i<=number;i++){
jumpSum=jumpBackStep1+jumpBackStep2;
jumpBackStep2=jumpBackStep1;//后退一级下次迭代时变成后退两级
jumpBackStep1=jumpSum;//当前台阶下次迭代时变成后退一级
}
return jumpSum;
}
}
You can’t perform that action at this time.