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
Developer articles
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 }}
javaCoderTeam
coding-interviews
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
coding-interviews
/
code
/
offer
/
src
/
Chap2
/
JumpFloor2.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
26 lines (25 loc) · 1.25 KB
master
Breadcrumbs
coding-interviews
/
code
/
offer
/
src
/
Chap2
/
JumpFloor2.java
Copy path
Top
File metadata and controls
Code
Blame
26 lines (25 loc) · 1.25 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
package
Chap2
;
/**
* 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。
* 求该青蛙跳上一个n级的台阶总共有多少种跳法。
*/
public
class
JumpFloor2
{
/**
* 到达1级台阶只有1种可能,到达2级台阶有2种可能;可记为f(1) = 1,f(2) = 2
* 要到达3级台阶,可以选择在1级台阶处起跳,也可以选择在2级台阶处起跳,也可直接跳到3级,所以只需到达1级台阶的可能情况 + 到达2级台阶的可能情况 + 1,即f(3) = f(2) +f(1) + 1
* 同理到达n级台阶,可以在n-1级台阶起跳,可在n-2、n-1、n-3...级台阶起跳,f(n) = f(n-1)+f(n-2)+f(n-3)...+1,
* 如果令f(n-n) = f(0) = 1,上式可表示为f(n) = f(n-1)+f(n-2)+f(n-3)...+f(n-n),
*
* !!还有种更好理解的思路:前n-1级台阶,每级台阶都有两种选择——跳到此或不跳到此,对于最后一级n级,没得选择,必须跳到这里
* 所以有2^(n-1)种跳法
*
* @param target 要到达的第n级台阶
* @return 达n级台阶总共的跳法可能
*/
public
int
jumpFloorII
(
int
target
) {
if
(
target
<=
0
) {
return
0
;
}
return
(
int
)
Math
.
pow
(
2
,
target
-
1
);
}
}
You can’t perform that action at this time.