LeetCode专题-Python实现之第7题:Reverse Integer
相关代码已经上传到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的更多相关文章
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第27题:Remove Element
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第20题:Valid Parentheses
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第9题:Palindrome Number
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第13题:Roman to Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第1题:Two Sum
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- 小马哥STM32课程系列
小马哥STM32课程系列 http://www.moore8.com/courses/1308
- mysql的复习
. 着重号,区分字段和关键字的符号 +号是运算的 起别名,其中的as可以省略 ifnull(expr1,expr2),expr1代表输入的字段,expr2代表如果输入的字段是null则为expr2 条 ...
- TypeScript 函数-函数类型
//指定参数类型 function add(x:number,y:number){ console.log("x:"+x); // reutrn(x+y); } //指定函数类型 ...
- python函数用法
一.定义函数 形参:函数完成一项工作所需要的信息,在函数定义时完成 实参:调用函数时传递给函数的信息 二.传递实参 1.位置实参:每个实参都关联到函数定义中的一个形参 示例: def describe ...
- redis的过期策略都有哪些?
1.面试题 redis的过期策略都有哪些?内存淘汰机制都有哪些?手写一下LRU代码实现? 2.面试官心里分析 1)老师啊,我往redis里写的数据怎么没了? 之前有同学问过我,说我们生产环境的redi ...
- js 单行注释
不可以: var a = 1;//这是注释 应当: var a = 1; //这是注释 1
- go语言指针理解
- 29 ArcMap许可服务器点击授权后无法进入下一步
系统描述:Windows server 2008 R2 ArcMap版本:10.6 系统要求各项都满足,包括补丁包都有,没有杀毒软件,ArcMap软件能安装上,但是到授权那步出问题 系统要求http ...
- 使用ANY和ALL条件
在比较运算符中,可以出现ALL和ANY,表示“全部”和“任一”,但是ALL和ANY不能单独使用,需要配合单行比较操作符>.>=.<.<=一起使用.其中: > ANY : ...
- Java作业九(2017-11-6)
/*圆的类*/ public class R { private double radius; // 构造方法,有参构造 public R(double radius) { this.radius = ...