leetcode--整数反转-创新互联
给出一个 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% 的用户
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章名称:leetcode--整数反转-创新互联
分享URL:http://tyjierui.cn/article/dcjped.html