{"id":901,"date":"2021-04-28T22:26:34","date_gmt":"2021-04-29T05:26:34","guid":{"rendered":"http:\/\/cywang.no-ip.org\/wordpress\/?p=901"},"modified":"2021-05-10T22:35:49","modified_gmt":"2021-05-11T05:35:49","slug":"66-plus-one","status":"publish","type":"post","link":"http:\/\/cywang.no-ip.org\/wordpress\/?p=901","title":{"rendered":"66. Plus One"},"content":{"rendered":"<p><a href=\"https:\/\/leetcode.com\/problems\/plus-one\/\" target=\"_blank\" rel=\"noopener\">66. Plus One<\/a><\/p>\n<p><!--more--><\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">class Solution:\r\n    def plusOne(self, digits: List[int]) -&gt; List[int]:\r\n        if len(digits) == 0:\r\n            return []\r\n        \r\n        # since the question is to increment 1 to the integer, set default carry to 1\r\n        carry = 1\r\n        # iterate the list in reverse order\r\n        for i in range(len(digits) - 1, -1, -1):\r\n            # new_value represents i-th element + carry\r\n            new_value = digits[i] + carry\r\n            # if new_value equals to 10, this means we need to add 1 to the next digit\r\n            if new_value == 10:\r\n                digits[i] = 0\r\n                carry = 1\r\n            else:\r\n                # carry = 0 now, done with iteration and return the list\r\n                digits[i] = new_value\r\n                return digits\r\n        if carry == 1:\r\n            # append 1 in front of the list\r\n            digits.insert(0, carry)\r\n        return digits<\/code><\/pre>\n<p>Time Complexity: O(N)<\/p>\n<p>Space Complexity: O(N)<\/p>\n<hr \/>\n<h1>Notes:<\/h1>\n<p>\u7a0b\u5f0f\u6d41\u7a0b<\/p>\n<ul>\n<li>\u56e0\u70ba\u984c\u76ee\u63d0\u5230 the most significant digit\u662f\u9019\u500blist\u7684\u958b\u59cb\uff0c\u6240\u4ee5\u5728\u505a\u52a01\u904b\u7b97\u7684\u6642\u5019\uff0c\u8981\u5f9elist\u7684\u6700\u5f8c\u4e00\u500b\u4f4d\u7f6e\u958b\u59cb\u8dd1\u8d77<\/li>\n<li>\u7406\u8ad6\u4e0a\u4e00\u958b\u59cb\u7684carry\u61c9\u8a72\u662f0\uff0c\u4f46\u662f\u6211\u5011\u53ea\u9700\u8981\u5728\u6700\u5f8c\u4e00\u500b\u6578\u5b57\u52a01\uff0c\u70ba\u4e86\u7c21\u5316\u6d41\u7a0b\uff0c\u628a\u4e00\u958b\u59cb\u9810\u8a2d\u7684carry\u503c\u8a2d\u62101<\/li>\n<li>\u5728iterate list\u7684\u6642\u5019\uff0c\u6bcf\u6b21\u90fd\u5148\u628a\u76ee\u524d\u7684\u6578\u503c\u52a0\u4e0acarry\uff0c\u4e26\u5b58\u5230\u4e00\u500b\u66ab\u5b58\u7684\u8b8a\u6578 (new_value)\n<ul>\n<li>\u5982\u679c new_value \u7b49\u65bc10\uff0c\u8868\u793a\u9700\u8981\u9032\u4f4d\uff1a\u628a\u8a72\u4f4d\u6578\u8a2d\u62100\u4e14carry = 1<\/li>\n<li>\u5982\u679c new_value \u4e0d\u7b49\u65bc10\uff0c\u8868\u793a\u4e0d\u9700\u8981\u9032\u4f4d\uff0c\u76f4\u63a5\u628a new_value \u5beb\u56de\u8a72\u4f4d\u6578\u3002\u56e0\u70ba\u4e0d\u6703\u6709\u9032\u4f4d\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u56de\u50b3\u7d50\u679c<\/li>\n<\/ul>\n<\/li>\n<li>\u6700\u5f8c\u5982\u679ccarry\u7b49\u65bc1\uff0c\u8868\u793a\u539f\u672c\u7684\u4f4d\u6578\u4e0d\u5920\u8868\u793a\u9019\u500b\u6578\u5b57\uff0c\u56e0\u6b64\u8981\u5728\u9019\u500blist\u524d\u9762\u52a0\u4e0a1 (\u9032\u4f4d)<\/li>\n<\/ul>\n<p>\u7d00\u9304<\/p>\n<ul>\n<li>10\u5206\u9418\u5beb\u4e0b\u984c\u76ee\u5927\u7db1\u3001\u9700\u8981\u7684\u5047\u8a2d\u3001method\u7684input\u8207output<\/li>\n<li>10\u5206\u9418\u5beb\u4e0bpseudo code\uff0c\u4e26\u8f49\u6210Python code (\u56e0\u70ba\u9019\u984c\u76f8\u5c0d\u7c21\u55ae\uff0cpseudo code\u8207\u6700\u7d42\u53ef\u57f7\u884c\u7684\u7a0b\u5f0f\u78bc\u76f8\u5dee\u4e0d\u5927)\uff0c\u8209\u5e7e\u500b\u4f8b\u5b50\u7528\u624b\u5beb(\u6a21\u64ec)\u7684\u65b9\u5f0f\u8dd1\u904e\u4e00\u6b21\u7a0b\u5f0f<\/li>\n<li>3-5\u5206\u9418\u628a\u624b\u5beb\u7684\u7a0b\u5f0f\u78bc\u8f38\u5165Leetcode\u4e26\u57f7\u884c<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>\u5099\u8a3b<\/p>\n<ul>\n<li>\u624b\u5beb\u7684\u7248\u672c\u662f\u5c0d\u7684\uff0c\u4f46\u662f\u8f38\u5165\u5230Leetcode\u5f8c\u6709typo\u3002\u4e4b\u5f8c\u5728Leetcode\u4e0a\u57f7\u884c\u524d\uff0c\u53ef\u80fd\u9700\u8981\u518d\u6838\u5c0d\u4e00\u6b21<\/li>\n<li>\u5728\u5beb\u6d41\u7a0b\u89e3\u91cb\u7684\u6642\u5019\uff0c\u767c\u73fe\u5176\u5be6\u539f\u672c\u7a0b\u5f0f\u78bc\u7684\u5012\u6578\u7b2c4\u884c\u4e0d\u9700\u8981\u518d\u505a\u4e00\u6b21\u5224\u65b7\uff0c\u5176\u5be6\u53ef\u4ee5\u76f4\u63a5\u505ainsert<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>66. Plus One<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[39],"tags":[],"class_list":["post-901","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=901"}],"version-history":[{"count":3,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/901\/revisions"}],"predecessor-version":[{"id":915,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/901\/revisions\/915"}],"wp:attachment":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=901"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}