当前位置:网站首页>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
- idea大小写快捷键
- 二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
- Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
- 九齐单片机NY8B062D单按键控制4种LED状态
- 复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
- Flet tutorial 05 outlinedbutton basic introduction (tutorial includes source code)
- 实践示例理解js强缓存协商缓存
- go语言笔记(2)go一些简单运用
- idea配置标准注释
猜你喜欢
太方便了,钉钉上就可完成代码发布审批啦!
【深度学习】一文看尽Pytorch之十九种损失函数
Flet教程之 04 FilledTonalButton基础入门(教程含源码)
What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
Related concepts of federal learning and motivation (1)
二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
原来这才是 BGP 协议
Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
Practical examples of node strong cache and negotiation cache
C # better operation mongodb database
随机推荐
Is it necessary to apply for code signing certificate for software client digital signature?
Record the online bug solving list (unfinished to be continued 7/4)
idea大小写快捷键
阿里测试师用UI自动化测试实现元素定位
Taishan Office Technology Lecture: about the order of background (shading and highlighting)
电脑共享打印机拒绝访问要怎么办
Six stones programming: about code, there are six triumphs
What ppt writing skills does the classic "pyramid principle" teach us?
What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
Automatic generation of interface automatic test cases by actual operation
奏响青春的乐章
九齐NY8B062D MCU规格书/datasheet
Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
What financial products can you buy with a deposit of 100000 yuan?
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
实践示例理解js强缓存协商缓存
PermissionError: [Errno 13] Permission denied: ‘data.csv‘
Reinforcement learning - learning notes 2 | value learning
紫光展锐完成全球首个 5G R17 IoT NTN 卫星物联网上星实测