首页 > 其他 > 详细

leetcode7 Reverse Integer

时间:2020-02-19 21:24:26      阅读:56      评论:0      收藏:0      [点我收藏+]
 1 """
 2 Given a 32-bit signed integer, reverse digits of an integer.
 3 Example 1:
 4 Input: 123
 5 Output: 321
 6 Example 2:
 7 Input: -123
 8 Output: -321
 9 Example 3:
10 Input: 120
11 Output: 21
12 """
13 """
14 为了防止溢出,本题需要先转str。再转int
15 还要考虑正负
16 考虑超出范围 返回0
17 用到了切片str[::-1],逆序
18 """
19 class Solution:
20     def reverse(self, x):
21         """
22         :type x: int
23         :rtype: int
24         """
25         x = int(str(x)[::-1]) if x >= 0 else - int(str(-x)[::-1])
26         return x if x < 2147483648 and x >= -2147483648 else 0

 

leetcode7 Reverse Integer

原文:https://www.cnblogs.com/yawenw/p/12333005.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!