{"id":94,"date":"2017-07-26T19:53:53","date_gmt":"2017-07-26T11:53:53","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=94"},"modified":"2018-11-27T15:56:02","modified_gmt":"2018-11-27T07:56:02","slug":"7-reverse-integer","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=94","title":{"rendered":"7. Reverse Integer"},"content":{"rendered":"<p>Given a 32-bit signed integer, reverse digits of an integer.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre><strong>Input:<\/strong> 123\r\n<strong>Output:<\/strong> 321\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre><strong>Input:<\/strong> -123\r\n<strong>Output:<\/strong> -321\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre><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<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def reverse(self, x):\r\n        &quot;&quot;&quot;\r\n        :type x: int\r\n        :rtype: int\r\n        &quot;&quot;&quot;\r\n        list_x = list(str(x))\r\n        temp = list_x[::-1]\r\n        if temp[len(temp)-1] is &quot;-&quot;:\r\n            temp.pop()\r\n            flag = &quot;-&quot;\r\n            for i in temp:\r\n                flag += i\r\n            return self.return_num(flag)\r\n        else:\r\n            flag = &quot;&quot;\r\n            for i in temp:\r\n                flag += i\r\n            return self.return_num(flag)\r\n\r\n    def return_num(self, num):\r\n        if int(num) &gt; (2 ** 31) or int(num) &lt; -(2 ** 31):\r\n            return 0\r\n        else:\r\n            return int(num)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a 32-bit signed integer, reverse digits of an int<a href=\"https:\/\/www.ccagml.com\/?p=94\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">7. Reverse Integer<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/94"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=94"}],"version-history":[{"count":2,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":97,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions\/97"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}