当前位置:网站首页>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;
}
};
边栏推荐
- 分析伦敦银走势图的技巧
- The problem of the maximum difference between the left and right maxima
- AP8022开关电源小家电ACDC芯片离线式开关电源IC
- 凌云出海记 | 沐融科技&华为云:打造非洲金融SaaS解决方案样板
- What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
- What financial products can you buy with a deposit of 100000 yuan?
- SSRS筛选器的IN运算(即包含于)用法
- MySQL statement execution details
- Integretee integrates into Moonriver through xcm, bringing enterprise class privacy solutions to its ecosystem
- What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
猜你喜欢

实操自动生成接口自动化测试用例

Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation

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

Flet教程之 08 AppBar工具栏基础入门(教程含源码)
node强缓存和协商缓存实战示例
Practice examples to understand JS strong cache negotiation cache

Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...

太方便了,钉钉上就可完成代码发布审批啦!
![[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born](/img/0b/73f0d98a6db813e54074abe199ed98.png)
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born

精选综述 | 用于白内障分级/分类的机器学习技术
随机推荐
哈希(Hash)竞猜游戏系统开发功能分析及源码
How does win11 search for wireless displays? Win11 method of finding wireless display device
长城证券开户安全吗 股票开户流程网上开户
Integretee integrates into Moonriver through xcm, bringing enterprise class privacy solutions to its ecosystem
idea恢复默认快捷键
Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
六石编程学:关于代码,有六个得意
字节测试工程师十年经验直击UI 自动化测试痛点
一文搞懂Go语言中文件的读写与创建
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
Selected review | machine learning technology for Cataract Classification / classification
左右最值最大差问题
LeetCode 871. 最低加油次数
Dynamic memory management
软件客户端数字签名一定要申请代码签名证书吗?
Talking about cookies of client storage technology
From automation to digital twins, what can Tupo do?
【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines