[Swift]LeetCode842. 将数组拆分成斐波那契序列 | Split Array into Fibonacci Sequence

Given a string S of digits, such as S = "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] <= 2^31 - 1, (that is, each integer fits 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.

Also, 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 S, or return [] if it cannot be done.

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: []
Explanation: The task is impossible.

Example 4:

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

Example 5:

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

Note:

  1. 1 <= S.length <= 200
  2. S contains only digits.


给定一个数字字符串 S,比如 S = "123456579",我们可以将它分成斐波那契式的序列 [123, 456, 579]

形式上,斐波那契式序列是一个非负整数列表 F,且满足:

  • 0 <= F[i] <= 2^31 - 1,(也就是说,每个整数都符合 32 位有符号整数类型);
  • F.length >= 3
  • 对于所有的0 <= i < F.length - 2,都有 F[i] + F[i+1] = F[i+2]成立。

另外,请注意,将字符串拆分成小块时,每个块的数字一定不要以零开头,除非这个块是数字 0 本身。

返回从 S 拆分出来的所有斐波那契式的序列块,如果不能拆分则返回 []

示例 1:

输入:"123456579"
输出:[123,456,579]

示例 2:

输入: "11235813"
输出: [1,1,2,3,5,8,13]

示例 3:

输入: "112358130"
输出: []
解释: 这项任务无法完成。

示例 4:

输入:"0123"
输出:[]
解释:每个块的数字不能以零开头,因此 "01","2","3" 不是有效答案。

示例 5:

输入: "1101111"
输出: [110, 1, 111]
解释: 输出 [11,0,11,11] 也同样被接受。

提示:

  1. 1 <= S.length <= 200
  2. 字符串 S 中只含有数字。


待完善

 1 class Solution {
 2     func splitIntoFibonacci(_ S: String) -> [Int] {
 3         var res:[Int] = [Int]()
 4         backtracking(S, 0, &res)
 5         return res
 6     }
 7
 8     func backtracking(_ S:String,_ start:Int,_ res:inout [Int]) -> Bool
 9     {
10         var n:Int = S.count
11         if start >= n && res.count >= 3
12         {
13             return true
14         }
15         var maxSplitSize:Int = (S[start] == "0") ? 1 : 10
16         for i in 1...maxSplitSize
17         {
18             if start + i <= S.count
19             {
20                 let num:Int = Int(S.subString(start, i)) ?? 0
21                 if num > Int(Int16.max)
22                 {
23                     continue
24                 }
25                 var sz:Int = res.count
26                 if sz >= 2 && res[sz - 1] + res[sz - 2] != num
27                 {
28                     continue
29                 }
30                 res.append(num)
31                 if backtracking(S, start + i, &res)
32                 {
33                     return true
34                 }
35                 res.popLast()
36             }
37
38         }
39         return false
40     }
41 }
42
43 extension String {
44     //subscript函数可以检索数组中的值
45     //直接按照索引方式截取指定索引的字符
46     subscript (_ i: Int) -> Character {
47         //读取字符
48         get {return self[index(startIndex, offsetBy: i)]}
49     }
50
51     // 截取字符串:指定索引和字符数
52     // - begin: 开始截取处索引
53     // - count: 截取的字符数量
54     func subString(_ begin:Int,_ count:Int) -> String {
55         let start = self.index(self.startIndex, offsetBy: max(0, begin))
56         let end = self.index(self.startIndex, offsetBy:  min(self.count, begin + count))
57         return String(self[start..<end])
58     }
59 }

原文地址:https://www.cnblogs.com/strengthen/p/10614994.html

时间: 2024-11-06 07:35:41

[Swift]LeetCode842. 将数组拆分成斐波那契序列 | Split Array into Fibonacci Sequence的相关文章

842. 将数组拆分成斐波那契序列

给定一个数字字符串 S,比如 S = "123456579",我们可以将它分成斐波那契式的序列 [123, 456, 579]. 形式上,斐波那契式序列是一个非负整数列表 F,且满足: 0 <= F[i] <= 2^31 - 1,(也就是说,每个整数都符合 32 位有符号整数类型):F.length >= 3:对于所有的0 <= i < F.length - 2,都有 F[i] + F[i+1] = F[i+2] 成立.另外,请注意,将字符串拆分成小块时,

最长斐波那契序列-LeetCode-873

英文版A sequence X_1, X_2, ..., X_n is fibonacci-like if: - n >= 3- X_i + X_{i+1} = X_{i+2} for all i + 2 <= n Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A.

斐波那契序列与跳台阶

转载请注明出处:http://blog.csdn.net/ns_code/article/details/25337983 剑指offer上的第就题,简单题,在九度OJ上测试通过. 主要注意以下几点: 1.用非递归实现,递归会超时 2.结果要用long long保存,不然会发生结果的溢出,从而得到负值 3.如果是在VC++6.0下编译的,long long是illegal的,要用_int64代替,同时输出的转化以字符也要用%64d代替%lld 时间限制:1 秒 内存限制:32 兆 题目描述: 大

斐波那契序列的求解

设斐波那契序列是f(n),其中 原文地址:https://www.cnblogs.com/gaoyixue/p/10464584.html

斐波那契序列的递归和非递归的实现

  斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci[1])以兔子繁殖为例子而引入,故又称为"兔子数列".   指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.--在数学上,斐波纳契数列以如下被以递归的方法定义:         F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2,n∈N*)       #include<iostream> u

斐波拉契序列几种实现

今天遇到一个面试题:输入n,如果n<=1,f(n)=1,其他f(n)=f(n-1)+f(n-2), 打印f(n)从1到n的值. public class Fibonacci { private int n; private StringBuilder sb; private String format; @Before public void init() { n = 10; sb = new StringBuilder(n); format = "f(%s) = "; } 直接

使用python实现斐波那契序列

def f1(a,b,stop):     if a == stop:         print(a)         return True     elif b == stop:         print(a,b,sep=" ",end="\n")         return True     elif a > stop:         print("无效的输入")         return False     else:

leetcode笔记:Climbing Stairs(斐波那契数列问题)

一.题目描述 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 题目的大意是,已知有n阶楼梯,每次只能爬1阶或2阶楼梯,问爬到第n阶楼梯共有几种爬法-_-||.题目可以看成是,设f(n)表示爬到第n 阶楼梯的方法数,为

二分查找和斐波那契查找

二分查找 说明:查找的数组或列表必须是有序的,若无序,先进行排序 复杂度:时间复杂度 O(log2n),空间复杂度O(n) C++源码(递归和非递归两个版本) #include <iostream> using namespace std; int a[] = { 1, 2, 3, 4, 5, 6, 8 }; int BinarySearch1(int l, int r, int value) { int mid = (l + r) / 2; if (l == r && a[l