当前位置:网站首页>LeetCode #7.整数反转
LeetCode #7.整数反转
2022-07-29 05:24:00 【张楚明ZCM】
题目截图

方法一:数学法
思路:
利用“%”取模,利用“/”除数。
注意:python默认是浮点算法,所以要加上int()强制转换为整数型。
同时题目要求输入输出的范围都必须是
,此处做两次判断。
同时要判断是否为负数。
输入的x为0时和正数情况一致,不做区分。
我分成两个方法。输入是负数时,先将负数转为正数,然后直接引用正数的方法反转,最后再将其变为负数。
class Solution:
def reverse(self, x: int) -> int:
if -2 ** 31 <= x <= 2 ** 31 - 1:
if x < 0:
x = -x
y = (-1) * self.reverse_Positive_number(x)
else:
y = self.reverse_Positive_number(x)
else:
y = 0
if -2 ** 31 <= y <= 2 ** 31 - 1:
pass
else:
y = 0
return y
def reverse_Positive_number(self, x: int) -> int:
y = 0
while x > 0:
temp = x % 10
x = int(x / 10)
y = y * 10 + temp
return y代码的优化:
是int型的取值范围,python中没有INT_MIN和INT_MAX,需要手动定义。直接在代码中写-2 ** 31等数值意义不明,应改为:
INT_MIN, INT_MAX = -2**31, 2**31 - 1python中两个斜杠“//”可以表示整除,不一定要使用int()来转换。
python3中取余向下取整数。
python3取模方法如下:
正数直接整除后取除法的余数
-x%y=z
找-x%y = -x- (比-x小的那个可以被y整除的负整数) =-x -(-n*y) ==n*y-x=z的模。
上段代码其实只需要判断返回的y是否在区间就可以了,当然,判断两次也无可厚非。
因为python3给负数取10的模也会返回
的结果,可以借此将模减去10,得到一个负数
如
,将
,再利用
,也就是
# Python3 的取模运算在 x 为负数时也会返回 [0, 9) 以内的结果,因此这里需要进行特殊判断
if x < 0 and digit > 0:
digit -= 10
最后优化的代码如下:
class Solution:
def reverse(self, x: int) -> int:
INT_MIN, INT_MAX = -2**31, 2**31 - 1
rev = 0
while x != 0:
# INT_MIN 也是一个负数,不能写成 rev < INT_MIN // 10
if rev < INT_MIN // 10 + 1 or rev > INT_MAX // 10:
return 0
digit = x % 10
if x < 0 and digit > 0:
digit -= 10
x = (x - digit) // 10
rev = rev * 10 + digit
return rev
或者
class Solution:
def reverse(self, x: int) -> int:
INT_MIN, INT_MAX = -2 ** 31, 2 ** 31 - 1
flag = 1 if x > 0 else -1
x_positive = abs(x)
y = 0
if INT_MIN <= x <= INT_MAX:
while x_positive > 0:
temp = x_positive % 10
x_positive = int(x_positive / 10)
y = y * 10 + temp
ret = flag * y
else:
ret = 0
if INT_MIN <= ret <= INT_MAX:
pass
else:
ret = 0
return ret方法二:利用字符串取反
设置一个标记flag,若x>0则为1,否则为-1
flag = 1 if x > 0 else -1让后将int型的x转变为str型,同时反转输出后再强制转变为int型
int(str())
利用abs()去x的绝对值
最后的结果乘以标记flag
ret = flag * int(str(abs(x))[::-1])最终结果
class Solution:
def reverse(self, x: int) -> int:
flag = 1 if x > 0 else -1
ret = flag * int(str(abs(x))[::-1])
if abs(ret) > 2 ** 31:
ret = 0
ret边栏推荐
- 爬虫Requests库的一些简单用法
- 2022暑初二信息竞赛学习成果分享2
- Based on STM32: couple interactive doll (design scheme + source code +3d drawing +ad circuit)
- 【RoboMaster】A板接收JY-ME01角度传感器数据--modebus协议&CRC软件校验
- 防爆倾角传感器应用于LNG液化天然气安全作业
- 2022 spring move - core technology FPGA development post pen test question (original question and experience)
- 【软件工程之美 - 专栏笔记】17 | 需求分析到底要分析什么?怎么分析?
- FPGA based: multi-target motion detection (hand-in-hand teaching ①)
- ML7 self study notes
- 无符号右移
猜你喜欢

基于msp430f2491的proteus仿真(实现流水灯)

八大排序-----------快速排序

SQLyog 安装和配置教程

2022 spring move - core technology FPGA post technical aspects (one side experience)

LeetCode #557.反转字符串中的单词 III

Reading papers on fake news detection (2): semi supervised learning and graph neural networks for fake news detection

给二维表添加时间序列索引

LeetCode #167.两数之和 II - 输入有序数组

智慧能源管理系统解决方案

CS5340国产替代DP5340多比特音频 A/D 转换器
随机推荐
SQLyog 安装和配置教程
Ml8 self study notes
物联网倾斜监测解决方案
传统模型预测控制轨迹跟踪——圆形轨迹(功能包已经更新)
LeetCode #3.无重复字符的最长子串
Jingwei Qili: OLED character display based on hmep060 (and Fuxi project establishment demonstration)
基于51单片机的DAC0832波形发生器
2022 spring recruit - Hesai technology FPGA technology post (one or two sides, collected from: Digital IC workers and FPGA Explorers)
从头安装MYSQL(MYSQL安装文档-解压版)
FPGA based: multi-target motion detection (hand-in-hand teaching ①)
FT232替代GP232RL USB-RS232转换器芯片国产化应用
#7110 数字走向2 题解
DP1332E多协议高度集成非接触式读写芯片
Hal library learning notes - 9 DMA
scanBasePackages扫包范围配置
SimpleFOC+PlatformIO踩坑之路
mavan中的plugin位置
ArduinoIDE + STM32Link烧录调试
【软件工程之美 - 专栏笔记】24 | 技术债务:是继续修修补补凑合着用,还是推翻重来?
Reading papers on fake news detection (2): semi supervised learning and graph neural networks for fake news detection