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.

答案
Typical backtracking problem, but need to pay attention to lots of details.

class Solution {
    public List<Integer> splitIntoFibonacci(String S) {
        List<Integer> list = new ArrayList<>();
        recur(S, 0, list);
        return list;
    }

    public boolean recur(String S, int curr_pos, List<Integer> list) {
        if(list.size() >= 3 && curr_pos == S.length()) return true;
        String max_int = Integer.toString(Integer.MAX_VALUE);
        // choose anything for current choice
        for(int i = curr_pos; i < S.length(); i++) {
            String choice = S.substring(curr_pos, i + 1);
            // Leading 0 is not allowed
            if(choice.charAt(0) == '0' && choice.length() > 1) continue;
            // Chec if choice can fit into an Integer object
            if(choice.length() > max_int.length() || (choice.length() == max_int.length() && (choice.compareTo(max_int)) > 0)) continue;

            int choice_int = Integer.parseInt(choice);
            int curr_size = list.size();
            if(curr_size <= 1) {
                list.add(choice_int);
            }
            else {
                int prev_sum = list.get(curr_size - 1) + list.get(curr_size - 2);
                if(prev_sum == choice_int) {
                    list.add(choice_int);
                }
                else if(prev_sum > choice_int){
                    continue;
                }
                else {
                    break;
                }
            }

            if(recur(S, i + 1, list)) return true;
            list.remove(list.size() - 1);
        }

        return false;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,449评论 0 10
  • 慌慌张张,匆匆忙忙,也许生活应该这样。 晚上客户请吃家宴,女主人做了一锅黔东南特色的辣子鸡。里边有魔域豆腐,这个我...
    G__t_G阅读 149评论 0 0
  • 沙苑汪建军阅读 156评论 0 2
  • 时间:2017年9月15日 地点:豆丁公寓 作者:阮博杰 下午在家里的沙发上昏睡着,仿佛感受到了很多画面,想起起不...
    阮博杰阅读 215评论 0 0