美文网首页
842. 将数组拆分成斐波那契序列

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

作者: 薄荷糖的味道_fb40 | 来源:发表于2019-05-06 14:51 被阅读0次

    给定一个数字字符串 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 <= S.length <= 200
    字符串 S 中只含有数字。
    

    思路:

    只能想到回溯了,遍历所有情况,符合条件的中止递归。有几个坑,要判断全0的时候,不是全0的话要判断正好踩在0上的时候,还有就是整形判断了,用stoll转成longlong型后与INT_MAX比较判断。代码写得有点烂,后面再考虑优化吧。

    class Solution {
    public:
        vector<int> splitIntoFibonacci(string S) {
            if(isallzero(S))
            {
                vector<int> res;
                for(int i=0;i<S.size();i++)
                {
                    res.push_back(0);
                }
                return res;
            }
            return helper(S,0,-1,-1);
        }
        
        bool isallzero(string S)
        {
            for(int i=0;i<S.size();i++)
            {
                if(S[i]!='0')
                    return false;
            }
            return true;
        }
        
        vector<int> helper(string S,int now,int first,int second)
        {
            vector<int> temp;
            if(now>=S.size())
            {
                return temp;
            }
            
            if(S.size()-now<13 && stoll(S.substr(now))<INT_MAX && stoll(S.substr(now))-first==second)
            {
                temp.push_back(stoll(S.substr(now)));
                return temp;
            }
                
            for(int i=0;now+i<S.size();i++)
            {
                if(stoll(S.substr(now,i+1))>INT_MAX)
                {
                    break;
                }
                else
                {
                    int value=(int)stoll(S.substr(now,i+1));
                    if(second==-1)
                    {
                        temp=helper(S,now+i+1,-1,value);
                    }
                    else if(first==-1)
                    {
                        temp=helper(S,now+i+1,second,value);
                    }
                    else
                    {
                        if(value-first==second)
                        {
                            temp=helper(S,now+i+1,second,value);
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                
                if(!temp.empty())
                {
                    temp.insert(temp.begin(),stoll(S.substr(now,i+1)));
                    break;
                }
                if(S[now]=='0')
                    break;
            }
            return temp;
        }
    };
    

    相关文章

      网友评论

          本文标题:842. 将数组拆分成斐波那契序列

          本文链接:https://www.haomeiwen.com/subject/kpnroqtx.html