当前位置:网站首页>LeetCode 7. 整数反转
LeetCode 7. 整数反转
2022-07-04 19:13:00 【_刘小雨】
给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。
如果反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] ,就返回 0。
假设环境不允许存储 64 位整数(有符号或无符号)。
示例 1:
输入:x = 123
输出:321
示例 2:
输入:x = -123
输出:-321
示例 3:
输入:x = 120
输出:21
示例 4:
输入:x = 0
输出: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;
}
};
// 不用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; /// 这行代码会溢出
x /= 10;
}
// if(re > INT_MAX) return 0;
// if(re < INT_MIN) return 0;
return re;
}
};
边栏推荐
- JS closure
- Hash quiz game system development how to develop hash quiz game system development (multiple cases)
- Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
- 工厂从自动化到数字孪生,图扑能干什么?
- Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
- Qt编写物联网管理平台38-多种数据库支持
- Oracle database, numbers Force 2 decimal places to display-Alibaba Cloud
- Flet教程之 08 AppBar工具栏基础入门(教程含源码)
- [in-depth learning] review pytoch's 19 loss functions
- [Beijing Xunwei] i.mx6ull development board porting Debian file system
猜你喜欢
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
NetCore3.1 Json web token 中间件
【深度学习】一文看尽Pytorch之十九种损失函数
Understand the reading, writing and creation of files in go language
什么叫内卷?
How to adapt your games to different sizes of mobile screen
Flet教程之 06 TextButton基础入门(教程含源码)
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
黄金k线图中的三角形有几种?
随机推荐
What ppt writing skills does the classic "pyramid principle" teach us?
Qt五子棋人机对战画棋子之QPainter的使用误区总结
[in-depth learning] review pytoch's 19 loss functions
word中插入圖片後,圖片上方有一空行,且删除後布局變亂
Flet教程之 05 OutlinedButton基础入门(教程含源码)
Length of the longest integrable subarray
Why is the maximum speed the speed of light
哈希(Hash)竞猜游戏系统开发功能分析及源码
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
Practice examples to understand JS strong cache negotiation cache
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
C server log module
How to adapt your games to different sizes of mobile screen
go语言笔记(4)go常用管理命令
Flet教程之 08 AppBar工具栏基础入门(教程含源码)
Alibaba testers use UI automated testing to achieve element positioning
What is involution?
idea配置标准注释
Cdga | six principles that data governance has to adhere to
tcp为啥是三次握手和四次挥手