{ "type": "code_options", "author": "csdn.net", "source": "solution.md", "exercise_id": "a29d470cdea24c7ab191ffe82e9aacf6", "keywords": "数学", "title": "整数反转", "desc": [ { "content": "\n

给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。

如果反转后整数超过 32 位的有符号整数的范围 [−231,  231 − 1] ,就返回 0。

假设环境不允许存储 64 位整数(有符号或无符号)。

 

示例 1:

输入:x = 123
输出:
321

示例 2:

输入:x = -123
输出:
-321

示例 3:

输入:x = 120
输出:
21

示例 4:

输入:x = 0
输出:
0

 

提示:

", "language": "markdown" } ], "answer": [ { "content": "", "language": "python" } ], "prepared": [ [ { "content": "", "language": "python" } ], [ { "content": "", "language": "python" } ], [ { "content": "", "language": "python" } ] ], "template": { "content": "import math\nclass Solution:\n\tdef reverse(self, x: int) -> int:\n\t\tr = 0\n\t\ty = 0\n\t\tabs_x = abs(x)\n\t\tnegative = x < 0\n\t\twhile abs_x != 0:\n\t\t\tr = abs_x % 10\n\t\t\ty = y*10+r\n\t\t\tabs_x = int(math.floor(abs_x/10))\n\t\tif negative:\n\t\t\ty = -y\n\t\treturn 0 if (y > 2147483647 or y < -2147483648) else y\n# %%\ns = Solution()\nprint(s.reverse(x = 123))\nprint(s.reverse(x = -123))", "language": "python" }, "node_id": "dailycode-cc53b8c00c3b47e49acec679ed77a9a8", "license": "csdn.net", "created_at": 1637894161, "topic_link": "https://bbs.csdn.net/topics/600469815" }