leetcode 7 Reverse Interger(python实现)

leetcode 7

题目描述

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-integer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题目解析
  • 思路一

初始思路:利用列表、字符串两种数据结构来反转数字,注意负数的处理和溢出情况的考虑

(32位int类型的表示范围为 [ − 2 31 , 2 31 − 1 ] [-2^{31}, 2^{31}-1] [231,2311] [ − 2147483648 , 2147483647 ] [-2147483648, 2147483647] [2147483648,2147483647],其十六进制为 [ − 0 x 80000000 , 0 x 7 f f f f f f f ] [-0x80000000, 0x7fffffff] [0x80000000,0x7fffffff]

class Solution:
    def reverse(self, x: int) -> int:
        flag = 0
        result = list(str(x))
        if result[0] == '-':
            flag = 1
            result.remove('-')
        result = ''.join(result[::-1])
        result = int(result)
        if flag == 1:
            result = -result
        if result > 0x7fffffff or result < -0x80000000:
            return 0
        return result

这种方法需要用到多个内置函数

  • 思路二

利用数学原理,一步步弹出输入参数x的最后一位,将其推入到rev的后面,从而将数字的顺序逆转过来。(rev存储我们要得到的结果)

初始化:rev=0

弹出:
pop = x % 10
x = x / 10

推入:
rev = rev * 10 + pop

例:

输入123:
rev = 0
1.  pop = 123 % 10 = 3
    x = 123 / 10 = 12
    rev = 0 * 10 + 3 = 3
2.  pop = 12 % 10 = 2
	x = 12 / 10 = 1
	rev = 3 * 10 + 2 = 32
3.  pop = 1 % 10 = 1
	x = 1 / 10 = 0
	rev = 32 * 10 + 1 = 321

得到下述代码:

class Solution:
    def reverse(self, x: int) -> int:        
        MIN_VALUE = -0x80000000
        MAX_VALUE = 0x7fffffff
        rev = 0
        while (x != 0):
            pop = x - int(x / 10) * 10 # 取余
            x = int(x / 10) # 整除
            # 上面的取余和整数之所以要这么写,是考虑到负数的情况,因为python中整除是向下取圆整,当负数进行取余、整除的操作时,往往会得到与我们常识不同的答案,这里要注意
            if rev > MAX_VALUE / 10 or (rev == MAX_VALUE / 10 and pop > 7):
                return 0
            elif rev < MIN_VALUE / 10 or (rev == MIN_VALUE / 10 and pop < -8):
                return 0
            rev = rev * 10 + pop
        return rev

这种方法速度较快,其时间复杂度为 O ( l o g 10 N ) O(log_{10}N) O(log10N),且不必区分正数和负数两种情况

  • 思路三

    与思路二相仿,我们也一步步弹出x的最后一位,推入到结果中。这可以用这种数据结构来实现, 代码如下:

class Solution:
    def reverse(self, x: int) -> int:        
        MIN_VALUE = -0x80000000
        MAX_VALUE = 0x7fffffff
        stack = []
        x_list = list(str(x))
        flag = 0
        while x_list != []:
            item = x_list.pop()
            if item == '-':
                flag = 1
                continue
            stack.append(item)
        
        result = int(''.join(stack))
        if flag == 1:
            result = -1 * result
        if result < MIN_VALUE or result > MAX_VALUE:
            return 0
        return result
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值