当前位置:网站首页>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;
}
};
边栏推荐
- 精选综述 | 用于白内障分级/分类的机器学习技术
- LeetCode 7. 整数反转
- 易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
- Practice examples to understand JS strong cache negotiation cache
- 【解决方案】PaddlePaddle 2.x调用静态图模式
- node强缓存和协商缓存实战示例
- How does win11 search for wireless displays? Win11 method of finding wireless display device
- 电脑共享打印机拒绝访问要怎么办
- Sword finger offer II 80-100 (continuous update)
- Idea case shortcut
猜你喜欢
Flet教程之 04 FilledTonalButton基础入门(教程含源码)
Managed service network: application architecture evolution in the cloud native Era
NetCore3.1 Json web token 中间件
精选综述 | 用于白内障分级/分类的机器学习技术
Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
Summary of the mistakes in the use of qpainter in QT gobang man-machine game
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
测试员的算法面试题-找众数
Reinforcement learning - learning notes 2 | value learning
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
随机推荐
奏响青春的乐章
电脑共享打印机拒绝访问要怎么办
Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
What if the brightness of win11 is locked? Solution to win11 brightness locking
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
word中使用自动插入题注功能
From automation to digital twins, what can Tupo do?
剑指 Offer II 80-100(持续更新)
Idea configuration standard notes
MySQL中的日期时间类型与格式化方式
RFID仓储管理系统解决方案的优点
Automatic insertion of captions in word
Go notes (1) go language introduction and characteristics
阿里测试师用UI自动化测试实现元素定位
Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
So this is the BGP agreement
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
Record the online bug solving list (unfinished to be continued 7/4)
word中插入圖片後,圖片上方有一空行,且删除後布局變亂
Go language notes (2) some simple applications of go