842. The array is split into Fibonacci sequence

Given a numeric string S, such as S = "123456579", we can divide it into the Fibonacci sequence of formula [123, 456, 579].

Formally, the Fibonacci sequence is a type of non-negative integer list F, and meet:

0 <= F [i] < = 2 ^ 31 - 1, ( that is, are in line with each integer 32-bit signed integer type);
F.length> =. 3;
for all 0 <= i <F. length - 2, both F [i] + F [i + 1] = F [i + 2] holds.
Also, note that when a string is split into small pieces, each block numbers must not start with zero unless the block number is 0 itself.

S spun off from the return of all the Fibonacci sequence of Formula block can not be split if it returns [].

Example 1:

Input: "123456579"
Output: [123,456,579]
Example 2:

Input: "11235813"
Output: [1, 1,]
Example 3:

Enter: "112 358 130"
Output: []
Explanation: This task can not be completed.
Example 4:

Input: "0123"
Output: []
Explanation: each block numbers can not start with zero, "01", "2", "3" is not a valid answer.
Example 5:

Input: "1101111"
Output: [110, 1, 111]
Explanation: Output [11,0,11,11] are also accepted.
prompt:

1 <= S.length <= 200
string S contains only numbers.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/split-array-into-fibonacci-sequence
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

 

The code a little rotten, do for a long time, more than the number before 5:00 a few, really TM fuck.

 1 public class Solution {
 2     private String s;
 3     private int len = -1;
 4     private List<Integer> res;
 5     private int e1;
 6     private int e2;
 7     private int e3;
 8 
 9     private String isContinue(int m,int n,int o){
10         String r = null;
11         String a = s.substring(m, n);
12         String b = s.substring(n, o);
13         if (a.length() > 10 || b.length() > 10)
14             return "";
15         long a0 = Long.parseLong(a);
16         long b0 = Long.parseLong(b);
17         if (a0 > 2147483647 || b0 > 2147483647)
18             return "";
19         e1 = (int)a0;
20         e2 = (int)b0;
21         if (a0+b0 > 2147483647)
22             return "";
23         e3 = (int)(a0+b0);
24         String c = String.valueOf(a0 + b0);
25         if (a.charAt(0)=='0' && a.length()!=1 || b.charAt(0)=='0' && b.length() != 1)
26             r = "";
27         else if (c.length() > len-o)
28             r = "";
29         else if (!s.substring(o,o+c.length()).equals(c))
30             r = "";
31         return (r != null) ? "" : c;
32     }
33 
34     private boolean helper(int m, int n, int o){
35         if (isContinue(m,n,o).equals("")){
36             return false;
37         }
38 
39         for (int i = n; i < len; i++) {
40             for (int j = i+1; j < len; j++) {
41                 String c = isContinue(m, i, j);
42                 if (c.equals(""))
43                     continue;
44                 if (s.substring(j,j+c.length()).equals(c)){
45                     if (c.length() > len-j)
46                         return false;
47 
48                     res.add(e1);
49 
50                     if (c.length() == len-j) {
51                         if (e2 == e1 || res.get(res.size()-1) != e2)
52                             res.add(e2);
53                         res.add(e3);
54                         return true;
55                     }
56                     if (helper(i, j, j+c.length()))
57                         return true;
58 
59                     res.remove(res.size()-1);
60                 }
61             }
62         }
63         return false;
64     }
65     public List<Integer> splitIntoFibonacci(String S) {
66         s = S;
67         len = s.length();
68         res = new ArrayList<>();
69 
70         for (int i = 1; i < len; i++) {
71             for (int j = i+1; j < len; j++) {
72                 if (helper(0,i,j))
73                     return res;
74             }
75         }
76         return res;
77     }
78 
79     public static void main(String[] args) {
80         List<Integer> integers = new Solution().splitIntoFibonacci("3611537383985343591834441270352104793375145479938855071433500231900737525076071514982402115895535257195564161509167334647108949738176284385285234123461518508746752631120827113919550237703163294909");
81         System.out.println("integers = " + integers);
82     }
83 }

 

Guess you like

Origin www.cnblogs.com/yfs123456/p/11620933.html