LeetCode 每日算法 306 :Additive number

本文由本人@takooctopusLeetCode每日打卡签到,做点记录。

306. Additive number

问题描述

Additive number is a string whose digits can form additive sequence.

A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.

Given a string containing only digits '0'-'9', write a function to determine if it’s an additive number.

Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.

Example

Example 1:
Input: “112358”
Output: true

Explanation: The digits can form an additive sequence: 1, 1, 2, 3, 5, 8.
1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8

Example 2:
Input: “199100199”
Output: true

Explanation: The additive sequence is: 1, 99, 100, 199.
1 + 99 = 100, 99 + 100 = 199

Follow up:

How would you handle overflow for very large input integers?

Solution:


{{- code -}}

我们在这里反向地运用比较会比较方便

其中itertools.combinations(a, b)会返回所有长度为b的组合「序列」,其顺序按照原来排序。startswith(a,b)很简单地看a是不是以b开头。

最核心的一点是在比较第三个数时,我们直接采用前两个数的和,转化为字符串进行比较,可以有效的规避首位0带来的问题。同理我们直接比较b转化int再转化的str,可以规避第一次首位0问题。

最终只要位指针指向了末尾,我们就能够判断其是不是Additive Number了。