leetcode--整数反转
2019-05-06 18:12
621 查看
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
示例 1:
输入: 123输出: 321
示例 2:
输入: -123输出: -321
示例 3:
输入: 120输出: 21
注意:
假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231, 231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
class Solution: def reverse(self, x: int) -> int: if x > 0: xstr = str(x) if not xstr.endswith('0'): xlist = list(xstr) xlist.reverse() x = int(''.join(xlist)) else: xlist = list(xstr) xlist.pop() xlist.reverse() x = int(''.join(xlist)) if x > pow(2, 31): return 0 else: return x elif x == 0: return x else: x = x.__abs__() xstr = str(x) if not xstr.endswith('0'): xlist = list(xstr) xlist.reverse() x = int(''.join(xlist)) else: xlist = list(xstr) xlist.pop() xlist.reverse() x = int(''.join(xlist)) if x > pow(2, 31): return 0 else: return 0-x
执行用时 : 56 ms, 在Reverse Integer的Python3提交中击败了98.51% 的用户
内存消耗 : 13.1 MB, 在Reverse Integer的Python3提交中击败了90.48% 的用户
相关文章推荐
- Leetcode7.整数反转(python3)
- [Leetcode] reverse integer 反转整数
- leetcode 7. 整数反转(Java版)
- LeetCode 第七题 反转整数
- leetcode_7.整数反转(python)
- LeetCode7.python实现:整数反转问题☆
- Leetcode(C++)——7. 整数反转
- LeetCode给出一个 32 位的有符号整数,将这个整数中每位上的数字进行反转。
- (LeetCode)反转整数
- leetcode--整数反转
- Leetcode Reverse Integer 反转整数
- [leetcode] Reverse Integer 反转一个整数
- Leetcode题库-整数反转(java语言版)
- LeetCode7-整数反转
- leetcode——Reverse Integer 反转整数数字(AC)
- leetcode 7 使用字符串操作反转整数
- leetcode的python实现 刷题笔记7: 反转整数
- Leetcode:Reverse Integer ,整数反转
- 【LeetCode】- Reverse Integer(将一个整数反转)
- 【LeetCode】 #7:反转整数 C语言