当前位置:网站首页>LeetCode 7. Integer inversion
LeetCode 7. Integer inversion
2022-07-04 20:56:00 【_ Liu Xiaoyu】
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 [−231, 231 − 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
Code:
class Solution {
public:
int reverse(int x) {
long long re = 0;
while(x)
{
re = re * 10 + x % 10;
x /= 10;
}
if(re > INT_MAX) return 0;
if(re < INT_MIN) return 0;
return re;
}
};
// no need long long
class Solution {
public:
int reverse(int x) {
int re = 0;
while(x)
{
if(re > 0 && re > (INT_MAX - x % 10)/ 10) return 0;
if(re < 0 && re < (INT_MIN - x % 10) / 10) return 0;
re = re * 10 + x % 10; /// This line of code will overflow
x /= 10;
}
// if(re > INT_MAX) return 0;
// if(re < INT_MIN) return 0;
return re;
}
};
边栏推荐
- Related concepts of federal learning and motivation (1)
- [ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
- Talking about cookies of client storage technology
- idea插件
- Practical examples of node strong cache and negotiation cache
- 栈:如何实现有效括号的判断?
- go笔记(3)Go语言fmt包的用法
- What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
- MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
- 扩展你的KUBECTL功能
猜你喜欢
Related concepts of federal learning and motivation (1)
MySQL中的日期时间类型与格式化方式
What if win11u disk refuses access? An effective solution to win11u disk access denial
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
RFID仓储管理系统解决方案的优点
See how Tencent does interface automation testing
In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills
LeetCode+ 81 - 85 单调栈专题
hash 表的概念及应用
Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法
随机推荐
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
Go notes (3) usage of go language FMT package
Play the music of youth
Why is TCP three handshakes and four waves
2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
ACM组合计数入门
MySQL statement execution details
Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法
word中使用自动插入题注功能
Win11亮度被锁定怎么办?Win11亮度被锁定的解决方法
word中插入图片后,图片上方有一空行,且删除后布局变乱
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
Function analysis and source code of hash guessing game system development
Flet tutorial 07 basic introduction to popupmenubutton (tutorial includes source code)
Record the online bug solving list (unfinished to be continued 7/4)
What should I do if my computer sharing printer refuses access
Sword finger offer II 80-100 (continuous update)
Flet教程之 06 TextButton基础入门(教程含源码)
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
电脑怎么保存网页到桌面上使用