当前位置:网站首页>Sword finger offer frog jumps stairs
Sword finger offer frog jumps stairs
2022-07-24 00:56:00 【wzf6667】
Title Description
A frog can jump up at a time 1 Stepped steps , You can jump on it 2 level …… It can also jump on n level . Ask the frog to jump on one n How many jumps are there in the steps .( Abnormal jump stairs )
Their thinking
1. Mathematical induction
because n Stepped steps , The first step is n Jump : jump 1 level 、 jump 2 level 、 To jump n level
jump 1 level , be left over n-1 level , The rest of the jump is f(n-1)
jump 2 level , be left over n-2 level , The rest of the jump is f(n-2)
therefore f(n)=f(n-1)+f(n-2)+…+f(1)
because f(n-1)=f(n-2)+f(n-3)+…+f(1)
therefore f(n)=2*f(n-1)
Calculate directly according to the formula , Write recursion
2. Brute force
jump 10 Stairs , It can be divided into jumping 1 individual , jump 2 All the way to jump 10 individual , So the cycle .
If you jump one , There's more left 9 individual , Run recursively , Nine can be divided into jumps 1 Jump nine , Until if there is only one staircase left , Just go back to 1. In fact, these two ideas are essentially the same , Just the last one is calculating from 1 Jump to the n A formula is used in the addition of, while brute force cracking uses a loop , For example, I jump 10 individual , Will calculate from f(1) Add to f(9) Of , But the formula is calculated directly f(10)=2*f(10-1)
public class Solution {
public int JumpFloorII(int target) {
if(target==1){
return 1;
}
int temp=0;
for(int i =1;i<target;i++){
temp=temp+JumpFloorII(i);
}
return temp+1;
}
}
边栏推荐
- Summary of polynomial commitment schemes
- Hcia-01 initial understanding of the Internet
- 采坑websocket總結
- QT入门篇(2.1初入QT的开始第一个程序)
- What is promise? What are the benefits of promise
- Prometheus+node exporter+grafana monitoring server system resources
- 测试小码农也有大目标,最新BAT大厂面试题大总结(持续更新中...)
- Image processing 1:rgb888_ YCbCr444
- 【Flyway 介绍】
- Case error of MySQL branch statement
猜你喜欢

What is the function of the select... For UPDATE statement? Can you lock tables or rows?

What impact does the European "gas shortage" have on China?

Bean validation usage article ----05

Graphic pipeline (I) post-processing stage alpha test template test depth test mix

Case error of MySQL branch statement

Coloring old photos - deoldify get started quickly

Xilinx FPGA one way clock input two PLLs

Image processing 1:rgb888_ YCbCr444

落枕如何快速缓解

postman测试接口在URL配置正确的情况下出现404或者500错误
随机推荐
Idea hot deployment (hot load)
Tutorial on principles and applications of database system (051) -- MySQL query (XIII): using queries in DML statements
Graphic pipeline (I) post-processing stage alpha test template test depth test mix
WinVerifyTrust调用返回80096005错误,时间戳签名或证书无法验证或已损坏
MySQL interview questions
入职3个月的测试员面临转正,领导:1年工作经验包装成5年,试用期淘汰
How to use SAP intelligent robotic process automation to automate Excel
Bean Validation自定义容器验证篇----06
Tutorial on principles and applications of database system (045) -- MySQL query (VII): aggregate function
vim常用命令
Solve the error: uncaught (in promise) navigationduplicated: avoided redundant navigation to current location:“
Interviewer: if the order is not paid within 30 minutes after it is generated, it will be automatically cancelled. How to realize it?
黑馬程序員-接口測試-四天學習接口測試-第四天-Postman讀取外部數據文件,讀取數據文件數據,iHRM項目實戰,員工管理模塊,添加員工,批量運行測試用例,生成測試報告,
采坑websocket總結
Are the top ten securities companies risky and safe to open accounts?
mysql 分支语句case报错
暑假第四周总结
[QNX Hypervisor 2.2用户手册]9 VM配置参考
Establishment of static route
《天幕红尘》笔记与思考(五)强势文化与弱势文化