当前位置:网站首页>leetcode842. Split the array into Fibonacci sequences
leetcode842. Split the array into Fibonacci sequences
2022-07-04 10:17:00 【JoesonChan】
subject
Given a string of numbers S, such as S = "123456579", We can divide it into Fibonacci sequences [123, 456, 579].
Formally , The Fibonacci sequence is a list of nonnegative integers F, And meet :
- 0 <= F[i] <= 2^31 - 1,( in other words , Every integer matches 32 Bit signed integer type );
- F.length >= 3;
- For all 0 <= i < F.length - 2, There are F[i] + F[i+1] = F[i+2] establish .
in addition , Please note that , When you split a string into small pieces , The number of each block must not start with zero , Unless this block is a number 0 In itself .
Return from S Any set of Fibonacci sequence blocks that are split out , If it can't be split, return [].
Example 1:
Input :"123456579"
Output :[123,456,579]
Example 2:
Input : "11235813"
Output : [1,1,2,3,5,8,13]
Example 3:
Input : "112358130"
Output : []
explain : The task cannot be accomplished .
Example 4:
Input :"0123"
Output :[]
explain : The number of each block cannot start with zero , therefore "01","2","3" Not a valid answer .
Example 5:
Input : "1101111"
Output : [110, 1, 111]
explain : Output [11,0,11,11] It's also accepted .
Tips :
- 1 <= S.length <= 200
- character string S There are only numbers in it .
public static List<Integer> _143_fabonaci(String text) {
List<Integer> result = new ArrayList<>();
boolean suc = dfsFabonaci(text.toCharArray(), 0, result);
// If you return true, explain result The array satisfies the Fibonacci sequence
return suc ? result : new ArrayList<>();
}
private static boolean dfsFabonaci(char[] chars, int index, List<Integer> result) {
if (index == chars.length && result.size() >= 3) {
return true;
}
if (index == chars.length) {
return false;
}
Long num = 0L;
int cursor = index;
while (cursor < chars.length) {
num = num * 10 + (chars[cursor] - '0');
if (num > Integer.MAX_VALUE) {
break;
}
// The current number exceeds the previous two , prune
if ((result.size() >= 2 && result.get(result.size() - 1) + result.get(result.size() - 2) < num.intValue())) {
break;
}
// The current list is less than two , Or the first two sums of the current list are equal to the current value , Recursion
if ((result.size() < 2 || result.get(result.size() - 1) + result.get(result.size() - 2) == num.intValue())) {
result.add(num.intValue());
boolean res = dfsFabonaci(chars, cursor + 1, result);
if (res) {
return true;
}
result.remove(result.size() - 1);
}
// If you encounter 0, There is no need to continue , prevent 0xx Numbers
if (num == 0L) {
break;
}
cursor++;
}
return false;
}
边栏推荐
- uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
- C # use gdi+ to add text to the picture and make the text adaptive to the rectangular area
- Exercise 9-3 plane vector addition (15 points)
- Exercise 8-10 output student grades (20 points)
- Hands on deep learning (40) -- short and long term memory network (LSTM)
- Dos:disk operating system, including core startup program and command program
- 华为联机对战如何提升玩家匹配成功几率
- System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
- AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers
- Realsense d435 d435i d415 depth camera obtains RGB map, left and right infrared camera map, depth map and IMU data under ROS
猜你喜欢
品牌连锁店5G/4G无线组网方案
A little feeling
Hands on deep learning (46) -- attention mechanism
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Hands on deep learning (42) -- bi-directional recurrent neural network (BI RNN)
Hands on deep learning (39) -- gating cycle unit Gru
直方图均衡化
Regular expression (I)
Machine learning -- neural network (IV): BP neural network
2. Data type
随机推荐
Dynamic address book
What is devsecops? Definitions, processes, frameworks and best practices for 2022
On Multus CNI
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
查看CSDN个人资源下载明细
Mmclassification annotation file generation
用数据告诉你高考最难的省份是哪里!
Sword finger offer 31 Stack push in and pop-up sequence
Baidu R & D suffered Waterloo on three sides: I was stunned by the interviewer's set of combination punches on the spot
libmysqlclient. so. 20: cannot open shared object file: No such file or directory
【Day2】 convolutional-neural-networks
Differences among opencv versions
Check 15 developer tools of Alibaba
Custom type: structure, enumeration, union
Latex error: missing delimiter (. Inserted) {\xi \left( {p,{p_q}} \right)} \right|}}
直方图均衡化
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
Application of safety monitoring in zhizhilu Denggan reservoir area
Network disk installation
Exercise 8-7 string sorting (20 points)