返回介绍

solution / 0800-0899 / 0842.Split Array into Fibonacci Sequence / README_EN

发布于 2024-06-17 01:03:34 字数 5492 浏览 0 评论 0 收藏 0

842. Split Array into Fibonacci Sequence

中文文档

Description

You are given a string of digits num, such as "123456579". We can split it into a Fibonacci-like sequence [123, 456, 579].

Formally, a Fibonacci-like sequence is a list f of non-negative integers such that:

  • 0 <= f[i] < 231, (that is, each integer fits in a 32-bit signed integer type),
  • f.length >= 3, and
  • f[i] + f[i + 1] == f[i + 2] for all 0 <= i < f.length - 2.

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

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

 

Example 1:

Input: num = "1101111"
Output: [11,0,11,11]
Explanation: The output [110, 1, 111] would also be accepted.

Example 2:

Input: num = "112358130"
Output: []
Explanation: The task is impossible.

Example 3:

Input: num = "0123"
Output: []
Explanation: Leading zeroes are not allowed, so "01", "2", "3" is not valid.

 

Constraints:

  • 1 <= num.length <= 200
  • num contains only digits.

Solutions

Solution 1

class Solution:
  def splitIntoFibonacci(self, num: str) -> List[int]:
    def dfs(i):
      if i == n:
        return len(ans) > 2
      x = 0
      for j in range(i, n):
        if j > i and num[i] == '0':
          break
        x = x * 10 + int(num[j])
        if x > 2**31 - 1 or (len(ans) > 2 and x > ans[-2] + ans[-1]):
          break
        if len(ans) < 2 or ans[-2] + ans[-1] == x:
          ans.append(x)
          if dfs(j + 1):
            return True
          ans.pop()
      return False

    n = len(num)
    ans = []
    dfs(0)
    return ans
class Solution {
  private List<Integer> ans = new ArrayList<>();
  private String num;

  public List<Integer> splitIntoFibonacci(String num) {
    this.num = num;
    dfs(0);
    return ans;
  }

  private boolean dfs(int i) {
    if (i == num.length()) {
      return ans.size() >= 3;
    }
    long x = 0;
    for (int j = i; j < num.length(); ++j) {
      if (j > i && num.charAt(i) == '0') {
        break;
      }
      x = x * 10 + num.charAt(j) - '0';
      if (x > Integer.MAX_VALUE
        || (ans.size() >= 2 && x > ans.get(ans.size() - 1) + ans.get(ans.size() - 2))) {
        break;
      }
      if (ans.size() < 2 || x == ans.get(ans.size() - 1) + ans.get(ans.size() - 2)) {
        ans.add((int) x);
        if (dfs(j + 1)) {
          return true;
        }
        ans.remove(ans.size() - 1);
      }
    }
    return false;
  }
}
class Solution {
public:
  vector<int> splitIntoFibonacci(string num) {
    int n = num.size();
    vector<int> ans;
    function<bool(int)> dfs = [&](int i) -> bool {
      if (i == n) {
        return ans.size() > 2;
      }
      long long x = 0;
      for (int j = i; j < n; ++j) {
        if (j > i && num[i] == '0') {
          break;
        }
        x = x * 10 + num[j] - '0';
        if (x > INT_MAX || (ans.size() > 1 && x > (long long) ans[ans.size() - 1] + ans[ans.size() - 2])) {
          break;
        }
        if (ans.size() < 2 || x == (long long) ans[ans.size() - 1] + ans[ans.size() - 2]) {
          ans.push_back(x);
          if (dfs(j + 1)) {
            return true;
          }
          ans.pop_back();
        }
      }
      return false;
    };
    dfs(0);
    return ans;
  }
};
func splitIntoFibonacci(num string) []int {
  n := len(num)
  ans := []int{}
  var dfs func(int) bool
  dfs = func(i int) bool {
    if i == n {
      return len(ans) > 2
    }
    x := 0
    for j := i; j < n; j++ {
      if j > i && num[i] == '0' {
        break
      }
      x = x*10 + int(num[j]-'0')
      if x > math.MaxInt32 || (len(ans) > 1 && x > ans[len(ans)-1]+ans[len(ans)-2]) {
        break
      }
      if len(ans) < 2 || x == ans[len(ans)-1]+ans[len(ans)-2] {
        ans = append(ans, x)
        if dfs(j + 1) {
          return true
        }
        ans = ans[:len(ans)-1]
      }
    }
    return false
  }
  dfs(0)
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文