当前位置:网站首页>Leetcode 7. integer inversion
Leetcode 7. integer inversion
2022-07-29 06:22:00 【Zhangchuming ZCM】
Power button | 7. Integer inversion
Title screenshot

Method 1 : Mathematical method
Ideas :
utilize “%” modulus , utilize “/” Divisor .
Be careful :python The default is floating point algorithm , So add int() Cast to integer .
At the same time, the topic requires that the input and output range must be
, Make two judgments here .
At the same time, it is necessary to judge whether it is a negative number .
Input x by 0 Time is consistent with positive numbers , Don't make a distinction .
I divide it into two methods . When the input is negative , First turn negative numbers into positive numbers , Then the method of directly referring to positive numbers is reversed , Finally, change it to a negative number .
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 yCode optimization :
yes int Value range of type ,python There is no INT_MIN and INT_MAX, You need to define it manually . Write... Directly in the code -2 ** 31 The significance of the equal value is unknown , Should be changed to :
INT_MIN, INT_MAX = -2**31, 2**31 - 1python Middle two slashes “//” It can express division , You don't have to use int() To convert .
python3 Take remainder from middle and take integer from bottom .
python3 The mold taking method is as follows :
Divide positive numbers directly and then take the remainder of the division
-x%y=z
look for -x%y = -x- ( Than -x The small one can be y Negative integer of integer division ) =-x -(-n*y) ==n*y-x=z The mold .
In fact, the previous code only needs to judge the returned y Whether it is in the interval is ok , Of course , There is nothing wrong with judging twice .
because python3 Take negative numbers 10 The module of will also return
Result , You can use this to subtract the module 10, Get a negative number
Such as
, take
, recycling
, That is to say 
# Python3 The modulo operation of x When it is negative, it will also return [0, 9) Results within , Therefore, special judgment is needed here
if x < 0 and digit > 0:
digit -= 10
Finally, the optimized code is as follows :
class Solution:
def reverse(self, x: int) -> int:
INT_MIN, INT_MAX = -2**31, 2**31 - 1
rev = 0
while x != 0:
# INT_MIN It's also a negative number , Can not write 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
perhaps
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 retMethod 2 : Use string inversion
Set a marker flag, if x>0 Then for 1, Otherwise -1
flag = 1 if x > 0 else -1Let's get back to int Type x Turn into str type , At the same time, reverse the output and then force it to int type
int(str())
utilize abs() Go to x The absolute value of
The final result is multiplied by the mark flag
ret = flag * int(str(abs(x))[::-1])final result
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边栏推荐
- STM32 printf问题总结 semihosting microLIB理解
- 一些工具,插件,软件链接分享给大家~
- 关于时间复杂度的个人看法
- 八大排序----------------冒泡排序
- 三国演义章节内容
- LeetCode #19.删除链表的倒数第N个结点
- Ml8 self study notes
- Logistic regression - project practice - credit card detection task (Part 2)
- 寒假集训总结 (1.23~1.28) [第一梯队]
- Ml8 self study notes LDA principle formula derivation
猜你喜欢

STM32 printf问题总结 semihosting microLIB理解

LeetCode #1.两数之和

Ml4 self study notes

Jingwei Qili: development of heart rate and blood oxygen module based on hmep060 (1: FPGA sends multi bit instructions)

Multithreading and concurrency

STM32FF030 替代国产单片机——DP32G030

【软件工程之美 - 专栏笔记】13 | 白天开会,加班写代码的节奏怎么破?

Ml6 self study notes

Ml self study notes 5

EPS32+Platform+Arduino 跑马灯
随机推荐
给二维表添加时间序列索引
SimpleFOC调参2-速度、位置控制
【软件工程之美 - 专栏笔记】20 | 如何应对让人头疼的需求变更问题?
FPGA based: moving target detection (schematic + source code + hardware selection, available)
简洁代码实现pdf转word文档
2022 spring move - core technology FPGA development post pen test question (original question and experience)
JVM内存结构
arduino uno错误分析avrdude: stk500_recv(): programmer is not responding
TB6600+stm32F407测试
Simple code to realize PDF to word document
LeetCode #189.轮转数组
【软件工程之美 - 专栏笔记】29 | 自动化测试:如何把Bug杀死在摇篮里?
三国演义章节内容
SimpleFOC调参3-PID参数整定攻略
【软件工程之美 - 专栏笔记】21 | 架构设计:普通程序员也能实现复杂系统?
Pit avoidance: about the interconnection of two hc-05 master-slave integrated Bluetooth modules, there is no connection problem
JUC集合类不安全
#9196 肿瘤面积 题解
From entry to soul: how to use tb6600 single chip microcomputer to control stepping motor with high precision (42/57)
Shell tool finalshell