{"id":6477,"date":"2020-03-14T00:38:00","date_gmt":"2020-03-14T07:38:00","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6477"},"modified":"2020-03-14T00:45:36","modified_gmt":"2020-03-14T07:45:36","slug":"leetcode-306-additive-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-306-additive-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 306. Additive Number"},"content":{"rendered":"\n<p>Additive number is a string whose digits can form additive sequence.<\/p>\n\n\n\n<p>A valid additive sequence should contain&nbsp;<strong>at least<\/strong>&nbsp;three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.<\/p>\n\n\n\n<p>Given a string containing only digits&nbsp;<code>'0'-'9'<\/code>, write a function to determine if it&#8217;s an additive number.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;Numbers in the additive sequence&nbsp;<strong>cannot<\/strong>&nbsp;have leading zeros, so sequence&nbsp;<code>1, 2, 03<\/code>&nbsp;or&nbsp;<code>1, 02, 3<\/code>&nbsp;is invalid.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> \"112358\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The digits can form an additive sequence: 1, 1, 2, 3, 5, 8. \n&nbsp;            1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> \"199100199\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The additive sequence is: 1, 99, 100, 199.&nbsp;\n&nbsp;            1 + 99 = 100, 99 + 100 = 199\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>num<\/code>&nbsp;consists only of digits&nbsp;<code>'0'-'9'<\/code>.<\/li><li><code>1 &lt;= num.length &lt;= 35<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DFS<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n^2)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\nusing SV = std::string_view;\nclass Solution {\npublic:\n  bool isAdditiveNumber(SV s) {    \n    auto add = [](SV s1, SV s2) {\n      const int l1 = s1.length(), l2 = s2.length();\n      string s3(max(l1, l2) + 1, '0');      \n      for (int i = 0; i < s3.size() - 1; ++i) {\n        int n1 = i < l1 ? s1[l1 - i - 1] - '0' : 0;\n        int n2 = i < l2 ? s2[l2 - i - 1] - '0' : 0;\n        s3[i] += n1 + n2;\n        if (s3[i] > '9') {\n          s3[i] -= 10;\n          ++s3[i + 1];\n        }\n      }\n      while (s3.back() == '0' && s3.length() > 1) s3.pop_back();\n      return string{rbegin(s3), rend(s3)};\n    };\n    \n    auto isValid = [](SV s) { return s.length() == 1 || s[0] != '0'; };\n    \n    function<bool(SV, SV, SV)> additive = [&](SV s1, SV s2, SV right) {\n      if (!isValid(s1) || !isValid(s2)) return false;      \n      string s3 = add(s1, s2);\n      const int l3 = s3.length();\n      if (right.substr(0, l3) != s3) return false;\n      if (right.length() == l3) return true;\n      return additive(s2, s3, right.substr(l3));\n    };\n    \n    for (int i = 1; i <= s.size(); ++i)\n      for (int j = 1; i + j <= s.size(); ++j)\n        if (additive(s.substr(0, i), s.substr(i, j), s.substr(i + j))) \n          return true;\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def isAdditiveNumber(self, num: str) -> bool:\n    n = len(num)\n    \n    def valid(s): return len(s) == 1 or s[0] != '0'\n    \n    def additive(s1, s2, right):\n      if not valid(s1) or not valid(s2): return False\n      s3 = str(int(s1) + int(s2))\n      if right.startswith(s3):\n        if right == s3: return True\n        return additive(s2, s3, right[len(s3):])\n      return False\n      \n    for l1 in range(1, n \/\/ 2 + 1):\n      for l2 in range(1, n + 1 - l1):\n        if additive(num[:l1], num[l1:l1+l2], num[l1+l2:]):\n          return True\n    return False\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Additive number is a string whose digits can form additive sequence. A valid additive sequence should contain&nbsp;at least&nbsp;three numbers. Except for the first two numbers,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[33,464,177,42,4],"class_list":["post-6477","post","type-post","status-publish","format-standard","hentry","category-searching","tag-dfs","tag-high-precision","tag-medium","tag-search","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6477"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6477\/revisions"}],"predecessor-version":[{"id":6480,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6477\/revisions\/6480"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}