{"id":5027,"date":"2019-04-10T22:05:36","date_gmt":"2019-04-11T05:05:36","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5027"},"modified":"2019-04-10T22:06:27","modified_gmt":"2019-04-11T05:06:27","slug":"leetcode-66-plus-one","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-66-plus-one\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 66. Plus One"},"content":{"rendered":"\n<p>Given a&nbsp;<strong>non-empty<\/strong>&nbsp;array of digits&nbsp;representing a non-negative integer, plus one to the integer.<\/p>\n\n\n\n<p>The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.<\/p>\n\n\n\n<p>You may assume the integer does not contain any leading zero, except the number 0 itself.<\/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> [1,2,3]\n<strong>Output:<\/strong> [1,2,4]\n<strong>Explanation:<\/strong> The array represents the integer 123.\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> [4,3,2,1]\n<strong>Output:<\/strong> [4,3,2,2]\n<strong>Explanation:<\/strong> The array represents the integer 4321.<\/pre>\n\n\n\n<p><strong>Solution: Big Integer<\/strong><\/p>\n\n\n\n<p>Process from right to left (lest significant to most signification). Use carry to indicate whether need +1 for next digit or not.<\/p>\n\n\n\n<p>Time complexity: O(n)<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++\">\nclass Solution {\npublic:\n  vector<int> plusOne(vector<int>& digits) {\n    int carry = 1;\n    for (int i = digits.size() - 1; i >= 0; --i) {\n      digits[i] += carry;\n      carry = digits[i] \/ 10;\n      digits[i] %= 10;\n    }\n    if (carry) digits.insert(begin(digits), carry);\n    return digits;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;non-empty&nbsp;array of digits&nbsp;representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[469,222,31],"class_list":["post-5027","post","type-post","status-publish","format-standard","hentry","category-math","tag-bigint","tag-easy","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5027","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=5027"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5027\/revisions"}],"predecessor-version":[{"id":5030,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5027\/revisions\/5030"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}