当前位置:网站首页>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;
}
};
边栏推荐
- See how Tencent does interface automation testing
- [today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
- 什么是区块哈希竞猜游戏系统开发?哈希竞猜游戏系统开发(案例成熟)
- 精选综述 | 用于白内障分级/分类的机器学习技术
- 九齐NY8B062D MCU规格书/datasheet
- Record the online bug solving list (unfinished to be continued 7/4)
- Huawei cloud store homepage banner resource bit application
- What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
- #夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
- Why is the maximum speed the speed of light
猜你喜欢

NetCore3.1 Json web token 中间件

What should I do if my computer sharing printer refuses access

Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines

Win11亮度被锁定怎么办?Win11亮度被锁定的解决方法

电脑共享打印机拒绝访问要怎么办

Flet教程之 04 FilledTonalButton基础入门(教程含源码)

分析伦敦银走势图的技巧

如何让你的小游戏适配不同尺寸的手机屏幕

黄金k线图中的三角形有几种?

Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
随机推荐
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
Flet教程之 08 AppBar工具栏基础入门(教程含源码)
FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
go笔记(1)go语言介绍以及特点
C # better operation mongodb database
What should I do if my computer sharing printer refuses access
js 闭包
Flet教程之 06 TextButton基础入门(教程含源码)
Form组件常用校验规则-1(持续更新中~)
Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
Flet tutorial 05 outlinedbutton basic introduction (tutorial includes source code)
漫谈客户端存储技术之Cookie篇
GVM使用
记一次 .NET 某工控数据采集平台 线程数 爆高分析
Why is the maximum speed the speed of light
太方便了,钉钉上就可完成代码发布审批啦!
So this is the BGP agreement
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
Selected review | machine learning technology for Cataract Classification / classification
LeetCode 871. Minimum refueling times