当前位置:网站首页>873. Length of the longest Fibonacci subsequence
873. Length of the longest Fibonacci subsequence
2022-07-18 16:55:00 【North_ dust】
today leetcode My daily question , The title link is :873. The length of the longest Fibonacci subsequence Here comes a solution :
First, use the data HashMap recorded , Then take two numbers from the array , And judge whether the sum of these two numbers is HashMap in , If in , Then continue to recursively judge downward , And record the maximum length , The code is as follows :
class Solution {
int maxTemp = -1;
int jTmp =-1;
int max = 0;
public int lenLongestFibSubseq(int[] arr) {
Map<Integer,Integer> map = new HashMap();
for (int i = 0; i < arr.length; i++) {
map.put(arr[i],i);
}
for (int i = 0; i < arr.length-2; i++) {
maxTemp = -1;
for (int j = i+1; j < arr.length-1; j++) {
int a = arr[i];
int b = arr[j];
jTmp = j;
getMaxTemp(map, a, b);
}
}
return max;
}
private void getMaxTemp(Map<Integer, Integer> map, int a, int b) {
int sum = a + b;
if(map.containsKey(sum)&& map.get(sum)> jTmp){
if(maxTemp ==-1){
maxTemp = 2;
}
a = b;
b =sum;
jTmp = map.get(sum);
maxTemp++;
max = Math.max(max,maxTemp);
getMaxTemp(map, a, b);
}else{
maxTemp=-1;
}
}
}
边栏推荐
- [Gu Yue 21 lecture] ROS introduction series (3) -- programming implementation of client and server + programming implementation of custom service data
- Scala 基础 (二):变量和数据类型
- 873. 最长的斐波那契子序列的长度
- 记录一个温度曲线的View
- Economic Daily praised Hongji, a leading RPA enterprise, for helping financial institutions with their digital transformation
- Apple commercial website blocks Firefox browser
- What does software testing need to learn? What skills do test engineers with an annual salary of 30w+ need to master?
- Oppo and Yijia will be banned in Germany? It turned out to be targeted by "patent hooligan" Nokia!
- OSPF(Open Shortest Path First开放式最短路径优先)防环以及计算错误带来的组网应用隐患
- The latest version 0.3 of Infini console, which has been waiting for a long time, is officially released!
猜你喜欢

考研人暑期行为大赏top5

SFF1602-MHCHXM(海矽美)二极管SFF1602

npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

Vscode 1.69 changes and concerns (three-way merge / terminal integration, etc.)

SAP Fiori Launchpad 上看不到任何 tile 应该怎么办?

Le quotidien économique loue hongji, l'entreprise leader de RPA, pour aider à la transformation numérique des institutions financières

JMeter如何使用MD5加密并且对body进行指纹签名

无996无内卷,乐视过上了“神仙日子”?

Best practice path for problem solving

历史上的今天:Mozilla 基金会正式成立;Enigma 密码机的首次工作;任天堂推出 FC 游戏机...
随机推荐
Linked list - [delete all duplicate elements]
【Xilinx AX7103 MicroBalze学习笔记7】MicroBlaze AXI4 接口之 DDR 读写实验
Create and generate WiFi QR code mobile phone scanning link
MindManager专业版2022思维导图工具
module ‘urllib‘ has no attribute ‘urlretrieve‘
INSET: Sentence Infilling with INter-SEntential Transformer
【刷题记录】12. 整数转罗马数字
Database daily question --- day 24: format department table
【手把手带你学UVM】~ 记录遇到的一切错误
3D激光SLAM:ALOAM---Ceres 优化部分及代码分析
Interviewer: how can you close an order without using a scheduled task?
SFF1004-MHCHXM(海矽美)二极管SFF1004
【古月21讲】ROS入门系列(3)——客户端Client、服务器Server的编程实现+自定义服务数据编程实现
Aardio - [library] graphical paging component library
UWA report uses tips. Did you get it? (the fifth bullet)
module ‘urllib‘ has no attribute ‘urlretrieve‘
Error-yolov5 pt转onnx报错
One stop Devops platform makes a big difference in development
Cv2.resize pit
如何利用pycharm制作一个简单的贪吃蛇小游戏