导航页-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. LOJ 6019

    挺没意思的题 全都读进去算一个每个阶乘的系数 然后算一遍每个数的系数 最后在质数处算一下答案 #include<bits/stdc++.h> using namespace std; #d ...

  2. # xrdp 在linux deploy 折腾记录

    xrdp 在linux deploy 折腾记录 1. xrdp connect fail vnc 不管用tiger or vnc4server都可以正常用,但xrdp无法连接 netstat -tnl ...

  3. 在 Vim 中优雅地查找和替换(转)

    总有人问我 Vim 中能不能查找,当然能!而且是超级强的查找! 这篇文章来详细介绍 Vim 中查找相关的设置和使用方法. 包括查找与替换.查找光标所在词.高亮前景/背景色.切换高亮状态.大小写敏感查找 ...

  4. 多阶段构建Docker镜像

    在Docker 17.05及更高的版本中支持支持一种全新的构建镜像模式:多阶段构建: 多阶段构建Docker镜像的最大好处是使构建出来的镜像变得更小: 目前常见的两个构建镜像的方式为: 1.直接使用某 ...

  5. [LeetCode] Magic Squares In Grid 网格中的神奇正方形

    A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, co ...

  6. 连接Redis_五种数据格式

    前面我们已经准备成功开启Redis服务,其端口号为6379,接下来我们就看看如何使用C#语言来操作Redis.就如MongoDB一样,要操作Redis服务,自然就需要下载C#的客户端,这里通过Nuge ...

  7. yum出现Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile解决方法

    yum出现Could not retrieve mirrorlist解决方法 Loaded plugins: fastestmirror, securityLoading mirror speeds ...

  8. electron 打包后node_modules 体积过于庞大

    问题产生原因:过多无用的模块被打包放入到程序中 解决: 删除 package-lock.json ,是由于这个文件导致打包时候放入过多文件,删除即可.

  9. MVVM简介与运用

    在介绍MVVM框架之前,先给大家简单介绍一下MVC.MVP框架(由于本博文主要讲解MVVM,所以MVC和MVP将简化介绍,如果需要我将在以后的博文中补充进来). MVC框架: M-Model : 业务 ...

  10. 怎么用JQUERY设置div背景图片?

    平常,在css里,我们写成 { background:url(....) ; } 如果需要写脚本, 则 function(){ .....; $(....).css("background- ...