{"id":1720,"date":"2022-01-10T10:14:34","date_gmt":"2022-01-10T02:14:34","guid":{"rendered":"http:\/\/139.9.1.231\/?p=1720"},"modified":"2022-01-10T10:14:37","modified_gmt":"2022-01-10T02:14:37","slug":"leetcodeday7","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/10\/leetcodeday7\/","title":{"rendered":"leetcodeday7 &#8211;\u6574\u6570\u53cd\u8f6c"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a 32 \u4f4d\u7684\u6709\u7b26\u53f7\u6574\u6570 x \uff0c\u8fd4\u56de\u5c06 x \u4e2d\u7684\u6570\u5b57\u90e8\u5206\u53cd\u8f6c\u540e\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u53cd\u8f6c\u540e\u6574\u6570\u8d85\u8fc7 32 \u4f4d\u7684\u6709\u7b26\u53f7\u6574\u6570\u7684\u8303\u56f4&nbsp;[\u2212231,&nbsp; 231&nbsp;\u2212 1] \uff0c\u5c31\u8fd4\u56de 0\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u73af\u5883\u4e0d\u5141\u8bb8\u5b58\u50a8 64 \u4f4d\u6574\u6570\uff08\u6709\u7b26\u53f7\u6216\u65e0\u7b26\u53f7\uff09\u3002<\/p>\n\n\n\n<p>\u793a\u4f8b 1\uff1a<\/p>\n\n\n\n<p>\u8f93\u5165\uff1ax = 123<br>\u8f93\u51fa\uff1a321<br>\u793a\u4f8b 2\uff1a<\/p>\n\n\n\n<p>\u8f93\u5165\uff1ax = -123<br>\u8f93\u51fa\uff1a-321<br>\u793a\u4f8b 3\uff1a<\/p>\n\n\n\n<p>\u8f93\u5165\uff1ax = 120<br>\u8f93\u51fa\uff1a21<br>\u793a\u4f8b 4\uff1a<\/p>\n\n\n\n<p>\u8f93\u5165\uff1ax = 0<br>\u8f93\u51fa\uff1a0<br>&nbsp;<\/p>\n\n\n\n<p>\u63d0\u793a\uff1a<\/p>\n\n\n\n<p>-231 &lt;= x &lt;= 231 &#8211; 1<\/p>\n\n\n\n<p>\u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09<br>\u94fe\u63a5\uff1ahttps:\/\/leetcode-cn.com\/problems\/reverse-integer<br>\u8457\u4f5c\u6743\u5f52\u9886\u6263\u7f51\u7edc\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u5b98\u65b9\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n\n\n\n<p>\u89e3\u9898\uff1a\u8fd9\u91cc\u9996\u5148\u60f3\u5230\u7684\u662f\u5148\u8f6c\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u53cd\u8f6c\u5b57\u7b26\u4e32\uff0c\u4f46\u5176\u5b9e\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u80fd\u6ee1\u8db3\u7cfb\u7edf32\u4f4d\u7684\u9650\u5236\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\u53c2\u8003\u5b98\u65b9\u89e3\u6cd5\uff0c\u662f\u4e00\u79cd\u7eaf\u6570\u5b66\u65b9\u6cd5\uff0c\u4ee3\u7801\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u6570\u5b66\u63a8\u5bfc\u590d\u6742\uff1a<\/p>\n\n\n\n<p>\u8bb0 rev \u4e3a\u7ffb\u8f6c\u540e\u7684\u6570\u5b57\uff0c\u4e3a\u5b8c\u6210\u7ffb\u8f6c\uff0c\u6211\u4eec\u53ef\u4ee5\u91cd\u590d\u300c\u5f39\u51fa\u300dx \u7684\u672b\u5c3e\u6570\u5b57\uff0c\u5c06\u5176\u300c\u63a8\u5165\u300drev \u7684\u672b\u5c3e\uff0c\u76f4\u81f3 x \u4e3a 0\u3002<\/p>\n\n\n\n<p>\u8981\u5728\u6ca1\u6709\u8f85\u52a9\u6808\u6216\u6570\u7ec4\u7684\u5e2e\u52a9\u4e0b\u300c\u5f39\u51fa\u300d\u548c\u300c\u63a8\u5165\u300d\u6570\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u6570\u5b66\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p>\/\/ \u5f39\u51fa x \u7684\u672b\u5c3e\u6570\u5b57 digit<br>digit = x % 10<br>x \/= 10<\/p>\n\n\n\n<p>\/\/ \u5c06\u6570\u5b57 digit \u63a8\u5165 rev \u672b\u5c3e<br>rev = rev * 10 + digit<\/p>\n\n\n\n<p>\u9898\u76ee\u9700\u8981\u5224\u65ad\u53cd\u8f6c\u540e\u7684\u6570\u5b57\u662f\u5426\u8d85\u8fc7 3232 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\u7684\u8303\u56f4 \\([-2^{31},2^{31}-1]\\)\uff0c\u4f8b\u5982 x=2123456789x=2123456789 \u53cd\u8f6c\u540e\u7684 \\(\\textit{rev}=9876543212>2^{31}-1=2147483647\\)\uff0c\u8d85\u8fc7\u4e86 32 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\u7684\u8303\u56f4\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5728\u300c\u63a8\u5165\u300d\u6570\u5b57\u4e4b\u524d\uff0c\u5224\u65ad\u662f\u5426\u6ee1\u8db3<\/p>\n\n\n\n<p>\u2212231\u2264<em>rev<\/em>\u22c510+<em>digit<\/em>\u2264231\u22121<\/p>\n\n\n\n<p>\u82e5\u8be5\u4e0d\u7b49\u5f0f\u4e0d\u6210\u7acb\u5219\u8fd4\u56de 0\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u9898\u76ee\u8981\u6c42\u4e0d\u5141\u8bb8\u4f7f\u7528 6464 \u4f4d\u6574\u6570\uff0c\u5373\u8fd0\u7b97\u8fc7\u7a0b\u4e2d\u7684\u6570\u5b57\u5fc5\u987b\u5728 3232 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\u7684\u8303\u56f4\u5185\uff0c\u56e0\u6b64\u6211\u4eec\u4e0d\u80fd\u76f4\u63a5\u6309\u7167\u4e0a\u8ff0\u5f0f\u5b50\u8ba1\u7b97\uff0c\u9700\u8981\u53e6\u5bfb\u4ed6\u8def\u3002<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"1024\" height=\"930\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-165-1024x930.png\" alt=\"\" class=\"wp-image-1727\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-165-1024x930.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-165-300x272.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-165-768x698.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-165.png 1210w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<figure class=\"wp-block-image size-large is-resized is-style-default\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-166-1024x534.png\" alt=\"\" class=\"wp-image-1728\" width=\"694\" height=\"361\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-166-1024x534.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-166-300x156.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-166-768x400.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-166-1536x801.png 1536w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-166.png 1558w\" sizes=\"(max-width: 694px) 100vw, 694px\" \/><\/figure>\n\n\n\n<p> \u6211\u4eec\u9700\u8981\u5728\u300c\u63a8\u5165\u300d\u6570\u5b57\u4e4b\u524d\uff0c\u5224\u65ad\u662f\u5426\u6ee1\u8db3 \uff1a<\/p>\n\n\n\n<p>$$<br>\\left\\lceil\\frac{-2^{31}}{10}\\right\\rceil \\leq \\operatorname{rev} \\leq\\left\\lfloor\\frac{2^{31}-1}{10}\\right\\rfloor<br>$$<\/p>\n\n\n\n<p>\u662f\u5426\u6210\u7acb\uff0c\u82e5\u4e0d\u6210\u7acb\u5219\u8fd4\u56de\u00a00<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc code=start\r\nclass Solution:\r\n    def reverse(self, x: int) -> int:\r\n        INT_MIN, INT_MAX = -2**31, 2**31 - 1\r\n\r\n        rev = 0\r\n        while x != 0:\r\n            # INT_MIN \u4e5f\u662f\u4e00\u4e2a\u8d1f\u6570\uff0c\u4e0d\u80fd\u5199\u6210 rev &lt; INT_MIN \/\/ 10\r\n            if rev &lt; INT_MIN \/\/ 10 + 1 or rev > INT_MAX \/\/ 10:\r\n                return 0\r\n            digit = x % 10\r\n            # Python3 \u7684\u53d6\u6a21\u8fd0\u7b97\u5728 x \u4e3a\u8d1f\u6570\u65f6\u4e5f\u4f1a\u8fd4\u56de &#91;0, 9) \u4ee5\u5185\u7684\u7ed3\u679c\uff0c\u56e0\u6b64\u8fd9\u91cc\u9700\u8981\u8fdb\u884c\u7279\u6b8a\u5224\u65ad\r\n            if x &lt; 0 and digit > 0:\r\n                digit -= 10\r\n\r\n            # \u540c\u7406\uff0cPython3 \u7684\u6574\u6570\u9664\u6cd5\u5728 x \u4e3a\u8d1f\u6570\u65f6\u4f1a\u5411\u4e0b\uff08\u66f4\u5c0f\u7684\u8d1f\u6570\uff09\u53d6\u6574\uff0c\u56e0\u6b64\u4e0d\u80fd\u5199\u6210 x \/\/= 10\r\n            x = (x - digit) \/\/ 10\r\n            rev = rev * 10 + digit\r\n        \r\n        return rev\r\n\r\n        # @lc code=end<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"794\" height=\"147\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-167.png\" alt=\"\" class=\"wp-image-1730\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-167.png 794w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-167-300x56.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-167-768x142.png 768w\" sizes=\"(max-width: 794px) 100vw, 794px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a 32 \u4f4d\u7684\u6709\u7b26\u53f7\u6574\u6570 x \uff0c\u8fd4\u56de\u5c06 x \u4e2d\u7684\u6570\u5b57\u90e8\u5206\u53cd\u8f6c\u540e\u7684\u7ed3\u679c\u3002 \u5982\u679c\u53cd\u8f6c\u540e\u6574\u6570\u8d85\u8fc7 32 \u4f4d\u7684\u6709 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/10\/leetcodeday7\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday7 &#8211;\u6574\u6570\u53cd\u8f6c<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1720"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=1720"}],"version-history":[{"count":6,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1720\/revisions"}],"predecessor-version":[{"id":1731,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1720\/revisions\/1731"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=1720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=1720"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=1720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}