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 文中代码为了不动官网提供的初始 ...
随机推荐
- 如何在HTML表格里定位到一行数据
业务需求: 在这样一个表格里,通过点击"确认"按钮,收集该行数据,向后台发送请求 解决办法 以该button为锚获取父节点,再由父节点获取各个元素的值 获取子元素又有很多办法,包括 ...
- BZOJ 4665
orz gery 一发rk1真有趣(其实我没想着常数优化 inline int sqr(int x){return 1ll*x*x%mo;} const int N=2011; int n,a[N], ...
- vue的父子组建之间的通信(-),基于props和$emit之间的传递
对于vue而言,以为其核心思想为前端组建化.所以组建之间的通信必不可少. 相信接触过Angularjs的童鞋都知道angularjs的控制器之间的通信机制. 1:父传子:官方的$broadcast() ...
- mysql练习
1.表关系 创建如下表格,并创建相关约束 ##(1)创建一个数据库 create database db2 default charset utf8; ##切换到db2数据库中 use db2 ##创 ...
- LeetCode笔记:140. Word Break II
题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...
- ReactJs和React Native的联系和差异
1,React Js的目的 是为了使前端的V层更具组件化,能更好的复用,它能够使用简单的html标签创建更多的自定义组件标签,内部绑定事件,同时可以让你从操作dom中解脱出来,只需要操作数据就会改变相 ...
- leetcode-查找和替换模式
一.题目描述 你有一个单词列表 words 和一个模式 pattern,你想知道 words 中的哪些单词与模式匹配.如果存在字母的排列 p ,使得将模式中的每个字母 x 替换为 p(x) 之后,我 ...
- 【二代示波器教程】第13章 RTX操作系统版本二代示波器实现
第13章 RTX操作系统版本二代示波器实现 本章教程为大家讲解RTX操作系统版本的二代示波器实现.主要讲解RTOS设计框架,即各个任务实现的功能,任务间的通信方案选择,任务栈,系统栈以及全局 ...
- [Swift]LeetCode831. 隐藏个人信息 | Masking Personal Information
We are given a personal information string S, which may represent either an email address or a phone ...
- [Swift]LeetCode956. 最高的广告牌 | Tallest Billboard
You are installing a billboard and want it to have the largest height. The billboard will have two ...