导航页-LeetCode专题-Python实现

相关代码已经上传到github:https://github.com/exploitht/leetcode-python

文中代码为了不动官网提供的初始几行代码内容,有一些不规范的地方,比如函数名大小写问题等等;更合理的代码实现参考我的github repo

1、读题

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

Note:

The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

这道题要求输入123返回321

输入-123返回-321

考虑32位有符号整型,溢出返回0

2、解题

看到逆序首先想到Python的各种序列类型可以轻松逆序,但是整型没有逆序的方法,咋办呢?转换成序列逆序然后转回来嘛,无非中间需要考虑正负号,考虑溢出。于是有了如下代码:

class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
# 将x的绝对值转换成字符串,逆序后转回整型数字
rev = int(str(abs(x))[::-1])
# 判断x和x转换后的结果是否能够不溢出,溢出则返回0
if abs(x) > pow(2, 31) or abs(rev) > pow(2, 31):
return 0
# 返回结果,如果x本身是负数,那么当前数字也变成负数
return rev if x > 0 else -rev

3、第二种解题思路

上面是通过py的序列反转来实现的,想想要不是py如此强大,某些语言可不能这样玩,比较常规的思路还是直接对数字操作,一位一位处理,于是有了如下解法

class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
""" y = 0
tmp_x = abs(x)
maxint = pow(2, 31)
while tmp_x:
# y不断进行当前位进10,拼接x低位的操作,这样x的最低位也就到了y的最高位
y = y * 10 + tmp_x % 10
# x要做的就是不断把个位拿出来
tmp_x = tmp_x / 10
if y > maxint or x > maxint:
return 0
return y if x > 0 else -y

LeetCode专题-Python实现之第7题:Reverse Integer的更多相关文章

  1. LeetCode专题-Python实现之第28题: Implement strStr()

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  2. LeetCode专题-Python实现之第27题:Remove Element

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  3. LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  4. LeetCode专题-Python实现之第21题:Merge Two Sorted Lists

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  5. LeetCode专题-Python实现之第20题:Valid Parentheses

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  6. LeetCode专题-Python实现之第9题:Palindrome Number

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. LeetCode专题-Python实现之第14题:Longest Common Prefix

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  8. LeetCode专题-Python实现之第13题:Roman to Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  9. LeetCode专题-Python实现之第1题:Two Sum

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

随机推荐

  1. nginx.conf 中php-ftp配置

    location ~ .php$ { root /home/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_par ...

  2. win10系统盘分多大合适?

    WIN10系统盘分多大合适,想必许多网友在装系统的时候都犹豫不觉吧,不过现在的硬盘基本上都是512G 1T的机械硬盘,固态硬盘基本都是128G以上,256G几乎成为标配,所以WIN10系统盘空间还是足 ...

  3. vue的环境安装(一node环境)

    话不多说都知道vue是基于node环境的.肯定是要安装node环境的 1.node官网下载https://nodejs.org/en/download/对应的版本我的是win的 2.双击下载下来的安装 ...

  4. JAVA_AesCBC纯净例子

    import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException ...

  5. spring-cloud-gateway负载普通web项目

    spring-cloud-gateway负载普通web项目 对于普通的web项目,也是可以通过spring-cloud-gateway进行负载的,只是无法通过服务发现. 背景 不知道各位道友有没有使用 ...

  6. python处理参数的getopt的使用

    在写脚本程序的时候需要添加一些额外的参数来实现脚本的附加功能或者增强功能,通常的做法是同sys.argv[i]直接来获取参数的值,但是这个比较局限,要求参数的输入一定要按照顺序. fileName = ...

  7. Unity进阶----DoTween及工程文件夹的建立(2018/11/12)

    DoTween 仅介绍部分常用用法,代码参上:(其它操作见官网:http://dotween.demigiant.com/documentation.php) using System.Collect ...

  8. ASP.NET Core应用的错误处理[4]:StatusCodePagesMiddleware中间件如何针对响应码呈现错误页面

    StatusCodePagesMiddleware中间件与ExceptionHandlerMiddleware中间件比较类似,它们都是在后续请求处理过程中“出错”的情况下利用一个错误处理器来完成最终的 ...

  9. MongoDB 分片键的选择与案例

    MongoDB版本:3.6 一.分片键类别 1.升序片键 升序片键例如:日期时间字段.自增字段. 2.随机分发片键 随机分发片键例如:用户名.邮件名.UUID.MD5值或者是其它的一些没有规律的值的列 ...

  10. [Swift]LeetCode91. 解码方法 | Decode Ways

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...