当前位置:网站首页>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;
}
};
边栏推荐
- Summary of the mistakes in the use of qpainter in QT gobang man-machine game
- Flet教程之 04 FilledTonalButton基础入门(教程含源码)
- In operation (i.e. included in) usage of SSRs filter
- What should I do if my computer sharing printer refuses access
- So this is the BGP agreement
- 长城证券开户安全吗 股票开户流程网上开户
- LeetCode 7. 整数反转
- What ppt writing skills does the classic "pyramid principle" teach us?
- Flet tutorial 05 outlinedbutton basic introduction (tutorial includes source code)
- 扩展你的KUBECTL功能
猜你喜欢

MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询

RFID仓储管理系统解决方案的优点
![NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]](/img/79/82763392e74d102921b4e8e601d4c6.png)
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]

What if win11u disk refuses access? An effective solution to win11u disk access denial

Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)

Reinforcement learning - learning notes 2 | value learning
![[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt](/img/0d/aa7f82fada743ea2ec23355ef954df.jpg)
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt

Common verification rules of form components -1 (continuously updating ~)

【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt

《动手学深度学习》(三) -- 卷积神经网络 CNN
随机推荐
Win11无法将值写入注册表项如何解决?
Flet tutorial 05 outlinedbutton basic introduction (tutorial includes source code)
idea配置标准注释
Automatic generation of interface automatic test cases by actual operation
Idea configuration standard notes
LeetCode+ 81 - 85 单调栈专题
Automatic insertion of captions in word
Flet教程之 07 PopupMenuButton基础入门(教程含源码)
tcp为啥是三次握手和四次挥手
实践示例理解js强缓存协商缓存
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
Advantages of semantic tags and block level inline elements
The problem of the maximum difference between the left and right maxima
So this is the BGP agreement
精选综述 | 用于白内障分级/分类的机器学习技术
BFC面试简述
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
Qt五子棋人机对战画棋子之QPainter的使用误区总结
剑指 Offer II 80-100(持续更新)
idea插件