题解 | #反转数字#

反转数字

http://www.nowcoder.com/practice/1a3de8b83d12437aa05694b90e02f47a

#
# 
# @param x int整型 
# @return int整型
#
class Solution:
    def reverse(self , x ):
        # write code here        
        n = 0

        flag = x>0
        x = abs(x)
        t = x
        while t!=0:
#             a = x/10
            t = t/10
            n += 1
        t = x
        res = 0
        while t!=0:            
            res += (t%10) * (10**(n-1))
            t = t/10
            n -= 1
        if not flag:
            res = (-1)*res
        if res > 2**31-1 or res < -1 * (2**31):
            return 0
        return res
全部评论

相关推荐

轻絵梨花泪沾衣:南泵,大少爷驾到通通闪开
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务