{"id":3927,"date":"2018-09-12T09:14:16","date_gmt":"2018-09-12T16:14:16","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3927"},"modified":"2018-09-12T09:15:37","modified_gmt":"2018-09-12T16:15:37","slug":"leetcode-7-reverse-integer","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-7-reverse-integer\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 7. Reverse Integer"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a 32-bit signed integer, reverse digits of an integer.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> 123\r\n<strong>Output:<\/strong> 321\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> -123\r\n<strong>Output:<\/strong> -321\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> 120\r\n<strong>Output:<\/strong> 21\r\n<\/pre>\n<p><strong>Note:<\/strong><br \/>\nAssume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [\u22122<sup>31<\/sup>,\u00a0 2<sup>31\u00a0<\/sup>\u2212 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.<\/p>\n<h1><strong>Solution: Simulation<\/strong><\/h1>\n<p>Reverse digit by digit. Be careful about the overflow and negative numbers (especially in Python)<\/p>\n<p>Time complexity: O(log(x)) ~ O(1)<\/p>\n<p>Space complexity: O(log(x)) ~ O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  int reverse(int x) {\r\n    int ans = 0;\r\n    while (x != 0) {\r\n      int r = x % 10;\r\n      if (ans &gt; INT_MAX \/ 10 || ans &lt; INT_MIN \/ 10) return 0;\r\n      ans = ans * 10 + r;\r\n      x \/= 10;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true\">class Solution {\r\n  public int reverse(int x) {\r\n    int ans = 0;\r\n    while (x != 0) {\r\n      int r = x % 10;\r\n      if (ans &gt; Integer.MAX_VALUE \/ 10 || ans &lt; Integer.MIN_VALUE \/ 10) return 0;\r\n      ans = ans * 10 + r;\r\n      x \/= 10;\r\n    }\r\n    return ans;\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \">class Solution:\r\n  def reverse(self, x):\r\n    min_val = -(2**31);\r\n    max_val = 2**31 - 1;\r\n    sign = -1 if x &lt; 0 else 1\r\n    x = abs(x)\r\n    ans = 0\r\n    while x != 0:            \r\n      ans = ans * 10 + (x % 10)\r\n      x \/\/= 10\r\n    ans *= sign\r\n    if ans &gt; max_val or ans &lt; min_val: return 0\r\n    return ans<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3:&#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":[93,222,31,246,179],"class_list":["post-3927","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-conversion","tag-easy","tag-math","tag-reverse","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3927","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=3927"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3927\/revisions"}],"predecessor-version":[{"id":3930,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3927\/revisions\/3930"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}