当前位置:网站首页>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;
}
};
边栏推荐
- How to solve the problem that win11 cannot write the value to the registry key?
- 九齐NY8B062D MCU规格书/datasheet
- 长城证券开户安全吗 股票开户流程网上开户
- 记一次重复造轮子(Obsidian 插件设置说明汉化)
- word中插入图片后,图片上方有一空行,且删除后布局变乱
- 科普达人丨一文看懂阿里云的秘密武器“神龙架构”
- 面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
- Flet教程之 06 TextButton基础入门(教程含源码)
- Automatic insertion of captions in word
- WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
猜你喜欢

面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧

电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法

托管式服务网络:云原生时代的应用体系架构进化

idea恢复默认快捷键

AP8022开关电源小家电ACDC芯片离线式开关电源IC

WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?

Automatic generation of interface automatic test cases by actual operation

See how Tencent does interface automation testing
![NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]](/img/79/82763392e74d102921b4e8e601d4c6.png)
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]

哈希表、哈希函数、布隆过滤器、一致性哈希
随机推荐
MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
Practice examples to understand JS strong cache negotiation cache
What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
长城证券开户安全吗 股票开户流程网上开户
After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
Automatic insertion of captions in word
What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
Reinforcement learning - learning notes 2 | value learning
NetCore3.1 Json web token 中间件
实践示例理解js强缓存协商缓存
Jekins initialization password not found or not found
Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t
记录线上bug解决list(未完待续7/4)
What is involution?
Cdga | six principles that data governance has to adhere to
接口設計時的一些建議
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
Go notes (3) usage of go language FMT package
LeetCode 871. Minimum refueling times
MySQL中的日期时间类型与格式化方式