当前位置:网站首页>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;
}
}
边栏推荐
- Establishment of static route
- C language: deep analysis of const keyword
- Off screen rendering & FBO
- Memory forensics nssctf otterctf 2018 (replay)
- [data mining engineer - written examination] Haier company in 2022
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
- [the 83rd fortnight of leetcode]
- An article teaches you the basic use of kubernetes
- QT入门篇(2.1初入QT的开始第一个程序)
- Coloring old photos - deoldify get started quickly
猜你喜欢

Starfish OS: create a new paradigm of the meta universe with reality as the link

The most basic code interpretation of C language

MySQL's heart index

Analysis of the advantages of the LAAS scheme of elephant swap led to strong performance of ETOKEN

Treatment of particle boundary collision

Redis common commands

Development of main applet for business card traffic near the map

Focus on microservices

An article teaches you the basic use of kubernetes

Creo 9.0 mouse button operation for model observation
随机推荐
Bean Validation自定义容器验证篇----06
Interviewer: if the order is not paid within 30 minutes after it is generated, it will be automatically cancelled. How to realize it?
What is the function of the select... For UPDATE statement? Can you lock tables or rows?
C language macro definition
Dynamic rip configuration
Tutorial on principles and applications of database system (045) -- MySQL query (VII): aggregate function
First knowledge of C language functions
[STM32] basic knowledge of serial communication
Client does not support authentication protocol requested by server; consider upgrading MySQL client
Chapter 1 water test --*offer
There are various signs that apple is expected to support AV1
MySQL common commands
黑馬程序員-接口測試-四天學習接口測試-第四天-Postman讀取外部數據文件,讀取數據文件數據,iHRM項目實戰,員工管理模塊,添加員工,批量運行測試用例,生成測試報告,
Prometheus+node exporter+grafana monitoring server system resources
Bert article translation
網絡系統實驗:ping不通的問題解决
【数据挖掘工程师-笔试】2022年海尔 公司
Is flush opening an account risky and safe?
mysql 分支语句case报错
Summary of polynomial commitment schemes