当前位置:网站首页>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;
}
};
边栏推荐
- 哈希表、哈希函数、布隆过滤器、一致性哈希
- Managed service network: application architecture evolution in the cloud native Era
- 易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
- Stack: how to realize the judgment of valid brackets?
- Flet tutorial 07 basic introduction to popupmenubutton (tutorial includes source code)
- How to solve the problem that win11 cannot write the value to the registry key?
- ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
- 记一次重复造轮子(Obsidian 插件设置说明汉化)
- LeetCode+ 81 - 85 单调栈专题
- Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法
猜你喜欢

Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"

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

Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法

What if the computer page cannot be full screen? The solution of win11 page cannot be full screen

Idea configuration standard notes

哈希表、哈希函数、布隆过滤器、一致性哈希

Flet教程之 08 AppBar工具栏基础入门(教程含源码)
![NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]](/img/79/82763392e74d102921b4e8e601d4c6.png)
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]

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

面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
随机推荐
NetCore3.1 Json web token 中间件
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
AP8022开关电源小家电ACDC芯片离线式开关电源IC
Record the online bug solving list (unfinished to be continued 7/4)
GVM use
FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
最长的可整合子数组的长度
Length of the longest integrable subarray
Sword finger offer II 80-100 (continuous update)
What should I do if my computer sharing printer refuses access
Common verification rules of form components -1 (continuously updating ~)
Oracle database, numbers Force 2 decimal places to display-Alibaba Cloud
Idea plug-in
哈希(Hash)竞猜游戏系统开发功能分析及源码
hash 表的概念及应用
FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
What ppt writing skills does the classic "pyramid principle" teach us?
Managed service network: application architecture evolution in the cloud native Era
Play the music of youth