当前位置:网站首页>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;
}
};
边栏推荐
- Go notes (1) go language introduction and characteristics
- Practice examples to understand JS strong cache negotiation cache
- 电脑怎么保存网页到桌面上使用
- Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
- Length of the longest integrable subarray
- 记一次重复造轮子(Obsidian 插件设置说明汉化)
- 针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
- 托管式服务网络:云原生时代的应用体系架构进化
- What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
- idea插件
猜你喜欢
How does win11 search for wireless displays? Win11 method of finding wireless display device
测试员的算法面试题-找众数
Win11亮度被锁定怎么办?Win11亮度被锁定的解决方法
Ten years' experience of byte test engineer directly hits the pain point of UI automation test
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
字节测试工程师十年经验直击UI 自动化测试痛点
hash 表的概念及应用
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
What if win11u disk refuses access? An effective solution to win11u disk access denial
Practice examples to understand JS strong cache negotiation cache
随机推荐
Idea plug-in
Automatic insertion of captions in word
JS closure
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
go语言笔记(4)go常用管理命令
Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
What if the brightness of win11 is locked? Solution to win11 brightness locking
Flet教程之 07 PopupMenuButton基础入门(教程含源码)
hash 表的概念及应用
PermissionError: [Errno 13] Permission denied: ‘data.csv‘
实践示例理解js强缓存协商缓存
ACM组合计数入门
Function analysis and source code of hash guessing game system development
记一次重复造轮子(Obsidian 插件设置说明汉化)
什么是区块哈希竞猜游戏系统开发?哈希竞猜游戏系统开发(案例成熟)
Win11亮度被锁定怎么办?Win11亮度被锁定的解决方法
企业数字化转型最佳实践案例:基于云的数字化平台系统安全措施简介与参考
扩展你的KUBECTL功能
See how Tencent does interface automation testing
Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法