{"id":168,"date":"2018-05-13T22:07:40","date_gmt":"2018-05-13T14:07:40","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=168"},"modified":"2018-11-27T17:11:35","modified_gmt":"2018-11-27T09:11:35","slug":"415-add-strings","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=168","title":{"rendered":"415. Add Strings"},"content":{"rendered":"<p>Given two non-negative integers\u00a0<code>num1<\/code>\u00a0and\u00a0<code>num2<\/code>\u00a0represented as string, return the sum of\u00a0<code>num1<\/code>\u00a0and\u00a0<code>num2<\/code>.<\/p>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The length of both\u00a0<code>num1<\/code>\u00a0and\u00a0<code>num2<\/code>\u00a0is &lt; 5100.<\/li>\n<li>Both\u00a0<code>num1<\/code>\u00a0and\u00a0<code>num2<\/code>\u00a0contains only digits\u00a0<code>0-9<\/code>.<\/li>\n<li>Both\u00a0<code>num1<\/code>\u00a0and\u00a0<code>num2<\/code>\u00a0does not contain any leading zero.<\/li>\n<li>You\u00a0<b>must not use any built-in BigInteger library<\/b>\u00a0or\u00a0<b>convert the inputs to integer<\/b>\u00a0directly.<\/li>\n<\/ol>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def addStrings(self, num1, num2):\r\n        &quot;&quot;&quot;\r\n        :type num1: str\r\n        :type num2: str\r\n        :rtype: str\r\n\r\n        &quot;&quot;&quot;\r\n        index = len(num1) if len(num1) &lt; len(num2) else len(num2) up = 0 result = &quot;&quot; for i in range(index): a = int(num1[len(num1) - 1 - i]) + int(num2[len(num2)-1 - i]) + up up = 1 if a &gt; 9 else 0\r\n            result = str(a % 10) + result\r\n\r\n        other = num1 if len(num1) &gt; len(num2) else num2\r\n        for j in range(len(other) - index):\r\n            a = int(other[len(other) - 1 - index - j]) + up\r\n            up = 1 if a &gt; 9 else 0\r\n            result = str(a % 10) + result\r\n        if up:\r\n            result = str(up) + result\r\n        return result\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given two non-negative integers\u00a0num1\u00a0and\u00a0num2\u00a0represent<a href=\"https:\/\/www.ccagml.com\/?p=168\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">415. Add Strings<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/168"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=168"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions"}],"predecessor-version":[{"id":169,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions\/169"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}