当前位置:网站首页>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;
}
};
边栏推荐
- 哈希表、哈希函数、布隆过滤器、一致性哈希
- NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
- How to adapt your games to different sizes of mobile screen
- 哈希(Hash)竞猜游戏系统开发功能分析及源码
- 扩展你的KUBECTL功能
- 记一次重复造轮子(Obsidian 插件设置说明汉化)
- E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
- idea恢复默认快捷键
- Related concepts of federal learning and motivation (1)
- Automatic insertion of captions in word
猜你喜欢
What should I do if my computer sharing printer refuses access
一文搞懂Go语言中文件的读写与创建
剑指 Offer II 80-100(持续更新)
Flet教程之 04 FilledTonalButton基础入门(教程含源码)
AP8022开关电源小家电ACDC芯片离线式开关电源IC
LeetCode+ 81 - 85 单调栈专题
Flet教程之 06 TextButton基础入门(教程含源码)
Hands on deep learning (III) -- convolutional neural network CNN
Win11怎么搜索无线显示器?Win11查找无线显示器设备的方法
工厂从自动化到数字孪生,图扑能干什么?
随机推荐
Summary of the mistakes in the use of qpainter in QT gobang man-machine game
Qt五子棋人机对战画棋子之QPainter的使用误区总结
最长的可整合子数组的长度
LeetCode 7. 整数反转
左右最值最大差问题
CDGA|数据治理不得不坚持的六个原则
Reinforcement learning - learning notes 2 | value learning
Jiuqi ny8b062d MCU specification /datasheet
Flet教程之 08 AppBar工具栏基础入门(教程含源码)
看腾讯大老如何做接口自动化测试
Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
Hash quiz game system development how to develop hash quiz game system development (multiple cases)
九齐NY8B062D MCU规格书/datasheet
哈希表、哈希函数、布隆过滤器、一致性哈希
AP8022开关电源小家电ACDC芯片离线式开关电源IC
Why is the maximum speed the speed of light
MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
奏响青春的乐章
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
九齐单片机NY8B062D单按键控制4种LED状态