<p>You are given a string of digits <code>num</code>, such as <code>&quot;123456579&quot;</code>. We can split it into a Fibonacci-like sequence <code>[123, 456, 579]</code>.</p>

<p>Formally, a <strong>Fibonacci-like</strong> sequence is a list <code>f</code> of non-negative integers such that:</p>

<ul>
	<li><code>0 &lt;= f[i] &lt; 2<sup>31</sup></code>, (that is, each integer fits in a <strong>32-bit</strong> signed integer type),</li>
	<li><code>f.length &gt;= 3</code>, and</li>
	<li><code>f[i] + f[i + 1] == f[i + 2]</code> for all <code>0 &lt;= i &lt; f.length - 2</code>.</li>
</ul>

<p>Note that when splitting the string into pieces, each piece must not have extra leading zeroes, except if the piece is the number <code>0</code> itself.</p>

<p>Return any Fibonacci-like sequence split from <code>num</code>, or return <code>[]</code> if it cannot be done.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> num = &quot;1101111&quot;
<strong>Output:</strong> [11,0,11,11]
<strong>Explanation:</strong> The output [110, 1, 111] would also be accepted.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> num = &quot;112358130&quot;
<strong>Output:</strong> []
<strong>Explanation:</strong> The task is impossible.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> num = &quot;0123&quot;
<strong>Output:</strong> []
<strong>Explanation:</strong> Leading zeroes are not allowed, so &quot;01&quot;, &quot;2&quot;, &quot;3&quot; is not valid.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= num.length &lt;= 200</code></li>
	<li><code>num</code> contains only digits.</li>
</ul>

class Solution {
public:
  vector<int> splitIntoFibonacci(string num) {
    if (num.length() < 3)
      return vector<int>();

    if (num[0] == '0' && num[1] == '0') {
      vector<int> output{0, 0};

      for (int i = 2; i < num.length(); i++) {
        if (num[i] != '0')
          return vector<int>();

        output.push_back(0);
      }

      return output;
    }

    int l{static_cast<int>(num.length())};
    vector<int> n1s;

    if (num[0] == '0') {
      n1s.push_back(0);
    } else {
      int i{0};
      int n1{0};

      while (i + 2 < l && n1 <= (INT_MAX - (num[i] - '0')) / 10) {
        n1 = n1 * 10 + (num[i++] - '0');
        n1s.push_back(n1);
      }
    }

    for (int i = 0; i < n1s.size(); i++) {
      vector<int> n2s;

      if (num[i + 1] == '0') {
        n2s.push_back(0);
      } else {
        int j{i + 1};
        int n2{0};

        while (j + 1 < l && n2 <= (INT_MAX - (num[j] - '0')) / 10) {
          n2 = n2 * 10 + (num[j++] - '0');
          n2s.push_back(n2);
        }
      }

      for (int j = 0; j < n2s.size(); j++) {
        int n1{n1s[i]};
        int n2{n2s[j]};

        if (INT_MAX - n1 < n2)
          continue;

        vector<int> output{n1, n2};
        int k{i + j + 2};
        bool isFibonacci{true};

        do {
          int n3{0};
          bool n3Overflow{false};

          if (num[k] != '0') {
            while (k < l && n3 - n2 < n1 && !n3Overflow) {
              if ((INT_MAX - (num[k] - '0')) / 10 < n3) {
                n3Overflow = true;
              } else {
                n3 = n3 * 10 + (num[k++] - '0');
              }
            }
          } else {
            k++;
          }

          if (n3Overflow || n1 != n3 - n2) {
            isFibonacci = false;
          } else {
            output.push_back(n3);
            n1 = n2;
            n2 = n3;
          }
        } while (k < l && isFibonacci);

        if (isFibonacci)
          return output;
      }
    }

    return vector<int>();
  }
};
7900