当前位置:网站首页>LeetCode 7. Integer inversion
LeetCode 7. Integer inversion
2022-07-04 20:56:00 【_ Liu Xiaoyu】
To give you one 32 Signed integer of bit x , Return to x The result of reversing the number part in .
If the integer after inversion exceeds 32 The range of signed integers of bits [−231, 231 − 1] , Just go back to 0.
Suppose the environment doesn't allow storage 64 An integer ( With or without sign ).
Example 1:
Input :x = 123
Output :321
Example 2:
Input :x = -123
Output :-321
Example 3:
Input :x = 120
Output :21
Example 4:
Input :x = 0
Output :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;
}
};
// no need 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; /// This line of code will overflow
x /= 10;
}
// if(re > INT_MAX) return 0;
// if(re < INT_MIN) return 0;
return re;
}
};
边栏推荐
- Selected review | machine learning technology for Cataract Classification / classification
- Related concepts of federal learning and motivation (1)
- word中使用自动插入题注功能
- 九齐单片机NY8B062D单按键控制4种LED状态
- 电脑怎么保存网页到桌面上使用
- go语言笔记(2)go一些简单运用
- ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
- Idea plug-in
- 长城证券开户安全吗 股票开户流程网上开户
- Function analysis and source code of hash guessing game system development
猜你喜欢

Idea configuration standard notes

字节测试工程师十年经验直击UI 自动化测试痛点

Sword finger offer II 80-100 (continuous update)

Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"

From automation to digital twins, what can Tupo do?

RFID仓储管理系统解决方案的优点

How to adapt your games to different sizes of mobile screen

How does win11 search for wireless displays? Win11 method of finding wireless display device
实践示例理解js强缓存协商缓存

ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
随机推荐
Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法
Go notes (1) go language introduction and characteristics
idea大小写快捷键
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
Selected review | machine learning technology for Cataract Classification / classification
二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
From automation to digital twins, what can Tupo do?
GVM使用
浏览器渲染页面过程
mysql语句执行详解
奏响青春的乐章
语义化标签的优势和块级行内元素
QT writing the Internet of things management platform 38- multiple database support
测试员的算法面试题-找众数
什么是区块哈希竞猜游戏系统开发?哈希竞猜游戏系统开发(案例成熟)
扩展你的KUBECTL功能
BFC interview Brief
实践示例理解js强缓存协商缓存
哈希表、哈希函数、布隆过滤器、一致性哈希