leetcode7. 整数反转

给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。

示例 1:

输入: 123
输出: 321
 示例 2:

输入: -123
输出: -321
示例 3:

输入: 120
输出: 21
注意:

假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231,  231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-integer

local func=function ( d )
    local isOpposive=d<0 and true or false
    if isOpposive then
        d=-d
    end
    local result=0
    while d>0 do
        result=result*10+(d%10)

        d=math.floor(d/10)
        --这里要判断result/10是否大于类型最大值,如果等于最大值,那么还要判断d是否大于此类型的首位数字,然后return0
    end
    if isOpposive then
        result=-result
    end
    return math.floor(result)
end

print(func(123456789))

  

  

posted on 2020-03-02 14:30  stigerzergold  阅读(200)  评论(0编辑  收藏  举报

导航