当前位置:网站首页>Part 74 leetcode exercise (VII) 7 Integer inversion
Part 74 leetcode exercise (VII) 7 Integer inversion
2022-04-22 06:02:00 【Chinese bad ape】
The first 74 piece LeetCode Problem practice ( 7、 ... and ) 7. Integer inversion
1. Title Description
To give you one 32 Signed integer of bit x , Return to x The result of reversing the number part in .
If the integer after inversion exceeds 32 The range of signed integers of bits [−2^31 , 2^31 − 1] , Just go back to 0.
Suppose the environment doesn't allow storage 64 An integer ( With or without sign ).
Example 1:
Input :x = 123
Output :321
Example 2:
Input :x = -123
Output :-321
Example 3:
Input :x = 120
Output :21
Example 4:
Input :x = 0
Output :0
Tips :
-2^31 <= x <= 2^31 - 1
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/reverse-integer
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
2. Their thinking
There is nothing to say about this question , It is worth noting that int The scope of the
int The range is -2147483648-------2147483647
There are defined macros in the buckle INT_MAX and INT_MIN, But this figure seems to be used in many places , By the way .
Given is an integer x, We use a variable res Save the number after inversion
start res = 0;
Every time x A bit of p, namely p = x % 10,
Then remove the x A bit of ( Of course not , But to get x The number before the first digit , The number of digits is less than before ), namely x /= 10;
Judge if you add p after ,res Overflow or not ,
(1) Positive numbers
If res > INT_MAX / 10( namely res Greater than 214748364),
that res multiply 10 It overflowed ,(res = res * 10 + p), Go straight back to 0
If res be equal to INT_MAX( namely res be equal to 214748364),
So judge p Is it greater than 7, If it is greater than 7,
Then it will overflow (214748364 * 10 + 8 > 2147483647).
(1) negative
If res > INT_MIN / 10( namely res Greater than -214748364),
that res multiply 10 It overflowed ,(res = res * 10 + p), Go straight back to 0
If res be equal to INT_MIN( namely res be equal to -214748364),
So judge p Is less than 8, If it is less than 8,
Then it will overflow (-214748364 * 10 - 9 > -2147483649).
3. Code
int reverse(int x) {
int max = 2147483647;
int min = -2147483648;
int res = 0;
while(x != 0){
int p = x % 10;
x /= 10;
if((res > max / 10) || ((res == max / 10) && p > 7)){
return 0;
}
else if((res < min / 10) || ((res == min / 10) && p < -8)){
return 0;
}
else{
res = res*10+p;
}
}
return res;
}
result

4. Conclusion
Things have to be used more .
版权声明
本文为[Chinese bad ape]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220540391747.html
边栏推荐
- Telbot load balancing settings
- Apple CMS builds a video website and collects videos regularly
- Blue Bridge Cup Sprint - DFS
- Blue Bridge Cup 31 day sprint Day3
- VB操作excel 格式设置及打印页面设置(精简)
- RTL8367学习笔记2——网络配置操作扫盲
- Introduction to machine learning -- comparison and fancy indexing in numpy
- 第88篇 LeetCode剑指Offer动态规划(五)礼物的最大值
- Chessboard coverage problem (divide and conquer)
- 日常学习记录——解决graphviz中文乱码问题
猜你喜欢
随机推荐
Blue Bridge Cup 31 day sprint day23
12 - container - string
蓝桥杯嵌入式扩展板学习之DHT11
08 - program input and output
[2022 Ali security] real scene tampering image detection challenge final rank17 scheme sharing
ocilib库连接oracle
STM32学习笔记1——最简单的GPIO
08 - 程序的输入和输出
Apple CMS sets the local player ckplayer (version: ckplayerx)
11 - 流程控制-for循环
QT添加pri编译运行: error: fatal error: no input files问题解决
LeetCode 514. The road of freedom -- dynamic programming
meilisearch使用记录
QT中出现error: undefined reference to `Widget::SetTime()‘
第90篇 LeetCode剑指Offer动态规划(七)最长不包含重复字符的子字符串
stm32单片机与LD3320语音模块交互法二
11 - process control - for loop
c语言开发postgres自定义函数
05-变量及标识符
RTL8367学习笔记1——基础知识









