当前位置:网站首页>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;
}
边栏推荐
- leetcode1-3
- Hands on deep learning (45) -- bundle search
- PHP code audit 3 - system reload vulnerability
- View CSDN personal resource download details
- Realsense d435 d435i d415 depth camera obtains RGB map, left and right infrared camera map, depth map and IMU data under ROS
- Today's sleep quality record 78 points
- Work order management system OTRs
- 对于程序员来说,伤害力度最大的话。。。
- 使用 C# 提取 PDF 文件中的所有文字(支持 .NET Core)
- 7-17 crawling worms (15 points)
猜你喜欢
Latex error: missing delimiter (. Inserted) {\xi \left( {p,{p_q}} \right)} \right|}}
百度研发三面惨遭滑铁卢:面试官一套组合拳让我当场懵逼
Hands on deep learning (37) -- cyclic neural network
Summary of small program performance optimization practice
If the uniapp is less than 1000, it will be displayed according to the original number. If the number exceeds 1000, it will be converted into 10w+ 1.3k+ display
[200 opencv routines] 218 Multi line italic text watermark
Tables in the thesis of latex learning
Hands on deep learning (35) -- text preprocessing (NLP)
Hands on deep learning (42) -- bi-directional recurrent neural network (BI RNN)
【Day2】 convolutional-neural-networks
随机推荐
Button wizard business running learning - commodity quantity, price reminder, judgment Backpack
JDBC and MySQL database
Exercise 7-3 store the numbers in the array in reverse order (20 points)
Hands on deep learning (46) -- attention mechanism
Kotlin 集合操作汇总
Whether a person is reliable or not, closed loop is very important
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
xxl-job惊艳的设计,怎能叫人不爱
Hands on deep learning (45) -- bundle search
Latex arranges single column table pictures in double column format articles
Map container
5g/4g wireless networking scheme for brand chain stores
Legion is a network penetration tool
Exercise 9-1 time conversion (15 points)
PHP代码审计3—系统重装漏洞
The future education examination system cannot answer questions, and there is no response after clicking on the options, and the answers will not be recorded
Exercise 7-8 converting strings to decimal integers (15 points)
leetcode1-3
Differences among opencv versions
MongoDB数据日期显示相差8小时 原因和解决方案