{"id":4817,"date":"2019-02-10T01:12:20","date_gmt":"2019-02-10T09:12:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4817"},"modified":"2019-02-10T02:19:25","modified_gmt":"2019-02-10T10:19:25","slug":"leetcode-989-add-to-array-form-of-integer","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-989-add-to-array-form-of-integer\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 989. Add to Array-Form of Integer"},"content":{"rendered":"\n<p>For a non-negative integer&nbsp;<code>X<\/code>, the&nbsp;<em>array-form of&nbsp;<code>X<\/code><\/em>&nbsp;is an array of its digits in left to right order.&nbsp; For example, if&nbsp;<code>X = 1231<\/code>, then the array form is&nbsp;<code>[1,2,3,1]<\/code>.<\/p>\n\n\n\n<p>Given the array-form&nbsp;<code>A<\/code>&nbsp;of a non-negative&nbsp;integer&nbsp;<code>X<\/code>, return the array-form of the integer&nbsp;<code>X+K<\/code>.<\/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>A = [1,2,0,0], K = 34\n<strong>Output: <\/strong>[1,2,3,4]\n<strong>Explanation: <\/strong>1200 + 34 = 1234\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>A = [2,7,4], K = 181\n<strong>Output: <\/strong>[4,5,5]\n<strong>Explanation: <\/strong>274 + 181 = 455\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>A = [2,1,5], K = 806\n<strong>Output: <\/strong>[1,0,2,1]\n<strong>Explanation: <\/strong>215 + 806 = 1021\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>A = [9,9,9,9,9,9,9,9,9,9], K = 1\n<strong>Output: <\/strong>[1,0,0,0,0,0,0,0,0,0,0]\n<strong>Explanation: <\/strong>9999999999 + 1 = 10000000000\n<\/pre>\n\n\n\n<p><strong>Note\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= A.length &lt;= 10000<\/code><\/li><li><code>0 &lt;= A[i] &lt;= 9<\/code><\/li><li><code>0 &lt;= K &lt;= 10000<\/code><\/li><li>If&nbsp;<code>A.length &gt; 1<\/code>, then&nbsp;<code>A[0] != 0<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Solution: Simulation<\/h2>\nTime complexity: O(n)\nSpace complexity: O(n)\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, running time: 136 ms, 10.1 MB\nclass Solution {\npublic:\n  vector<int> addToArrayForm(vector<int>& A, int K) {\n    vector<int> ans;\n    ans.reserve(A.size() + 1);    \n    for (int i = A.size() - 1; i >= 0 || K > 0; --i) {\n      K += (i >= 0 ? A[i] : 0);\n      ans.push_back(K % 10);\n      K \/= 10;\n    }    \n    reverse(begin(ans), end(ans));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>For a non-negative integer&nbsp;X, the&nbsp;array-form of&nbsp;X&nbsp;is an array of its digits in left to right order.&nbsp; For example, if&nbsp;X = 1231, then the array form&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[222,464,31,179],"class_list":["post-4817","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-high-precision","tag-math","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4817","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=4817"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4817\/revisions"}],"predecessor-version":[{"id":4823,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4817\/revisions\/4823"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}