{"id":2177,"date":"2018-03-17T13:34:20","date_gmt":"2018-03-17T20:34:20","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2177"},"modified":"2018-03-17T13:36:32","modified_gmt":"2018-03-17T20:36:32","slug":"leetcode-415-add-strings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-415-add-strings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 415. Add Strings"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<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<h1><strong>Solution: Brute Force<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 13 ms\r\nclass Solution {\r\npublic:\r\n  string addStrings(string num1, string num2) {\r\n    if (num1.length() &lt; num2.length()) swap(num1, num2);\r\n    num1 = \"0\" + num1;\r\n    int l1 = num1.length();\r\n    int l2 = num2.length();\r\n    for (int i = 0; i &lt; l1 - 1; ++i) {\r\n      if (i &lt; l2) num1[l1 - i - 1] += (num2[l2 - i - 1] - '0');\r\n      if (num1[l1 - i - 1] &gt; '9') {\r\n        num1[l1 - i - 1] -= 10;\r\n        ++num1[l1 - i - 2];\r\n      }\r\n    }    \r\n    return (num1[0] != '0') ? num1 : num1.substr(1);\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given two non-negative integers\u00a0num1\u00a0and\u00a0num2\u00a0represented as string, return the sum of\u00a0num1\u00a0and\u00a0num2. Note: The length of both\u00a0num1\u00a0and\u00a0num2\u00a0is &lt; 5100. Both\u00a0num1\u00a0and\u00a0num2\u00a0contains only digits\u00a00-9. Both\u00a0num1\u00a0and\u00a0num2\u00a0does not contain any&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[260,222,4],"class_list":["post-2177","post","type-post","status-publish","format-standard","hentry","category-string","tag-bitint","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2177","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=2177"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2177\/revisions"}],"predecessor-version":[{"id":2181,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2177\/revisions\/2181"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}