当前位置:网站首页>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;
}
};
边栏推荐
- In operation (i.e. included in) usage of SSRs filter
- 阿里测试师用UI自动化测试实现元素定位
- 泰山OFFICE技术讲座:关于背景(底纹和高亮)的顺序问题
- Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
- BFC interview Brief
- How to solve the problem that win11 cannot write the value to the registry key?
- Stack: how to realize the judgment of valid brackets?
- [in-depth learning] review pytoch's 19 loss functions
- 更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
- 电脑共享打印机拒绝访问要怎么办
猜你喜欢

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

托管式服务网络:云原生时代的应用体系架构进化

Jiuqi ny8b062d MCU specification /datasheet

ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise

同事的接口文档我每次看着就头大,毛病多多。。。

科普达人丨一文看懂阿里云的秘密武器“神龙架构”

B2B mall system development of electronic components: an example of enabling enterprises to build standardized purchase, sale and inventory processes

How does the computer save web pages to the desktop for use
![[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt](/img/0d/aa7f82fada743ea2ec23355ef954df.jpg)
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt

Managed service network: application architecture evolution in the cloud native Era
随机推荐
电脑共享打印机拒绝访问要怎么办
黄金k线图中的三角形有几种?
What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
Lingyun going to sea | Wenhua online & Huawei cloud: creating a new solution for smart teaching in Africa
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
Win11怎么搜索无线显示器?Win11查找无线显示器设备的方法
Hash quiz game system development how to develop hash quiz game system development (multiple cases)
Talking about cookies of client storage technology
GVM使用
Taishan Office Technology Lecture: about the order of background (shading and highlighting)
长城证券开户安全吗 股票开户流程网上开户
go语言笔记(4)go常用管理命令
紫光展锐完成全球首个 5G R17 IoT NTN 卫星物联网上星实测
关于联邦学习和激励的相关概念(1)
Is it necessary to apply for code signing certificate for software client digital signature?
【深度学习】一文看尽Pytorch之十九种损失函数
Flet教程之 07 PopupMenuButton基础入门(教程含源码)
Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC