Class Solution

java.lang.Object
g0301_0400.s0306_additive_number.Solution

public class Solution extends Object
306 - Additive Number.<p>Medium</p> <p>An <strong>additive number</strong> is a string whose digits can form an <strong>additive sequence</strong>.</p> <p>A valid <strong>additive sequence</strong> should contain <strong>at least</strong> three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.</p> <p>Given a string containing only digits, return <code>true</code> if it is an <strong>additive number</strong> or <code>false</code> otherwise.</p> <p><strong>Note:</strong> Numbers in the additive sequence <strong>cannot</strong> have leading zeros, so sequence <code>1, 2, 03</code> or <code>1, 02, 3</code> is invalid.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> &ldquo;112358&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong></p> <pre><code> 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 </code></pre> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> &ldquo;199100199&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong></p> <pre><code> The additive sequence is: 1, 99, 100, 199. 1 + 99 = 100, 99 + 100 = 199 </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= num.length <= 35</code></li> <li><code>num</code> consists only of digits.</li> </ul> <p><strong>Follow up:</strong> How would you handle overflow for very large input integers?</p>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • isAdditiveNumber

      public boolean isAdditiveNumber(String num)