当前位置:网站首页>Leetcode integer exercises integer inversion
Leetcode integer exercises integer inversion
2022-07-28 22:25:00 【Mubaiang】
Problem description :
To give you one 32 Bit Signed integers 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 [−2^31^, 2^31^ − 1] , Just go back to 0. Suppose the environment doesn't allow storage 64 An integer ( With or without sign )
Problem description
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
Tips :
-2^31 <= x <= 2^31 - 1
Analysis of problem solving ideas :
Tips : My idea of solving the problem is to first Int Convert to string processing , Then convert the string to Int Result output of type , Among them, it is necessary to judge the output results , When the output number is before 9 position >147483647, The first 10 position >=2 when , Then the output value exceeds Int Range (
Be careful not to use long Type save results), Finally, the output result is the symbol 10
- Be careful : there long yes linux The sum of vs Under the long The scope is different
Solution :
#include <stdlib.h>
class Solution {
public:
string intToString(int x)//Int Turn the string
{
char intStr[1024] = {
0};
sprintf(intStr,"%d", x);
return string(intStr);
}
int RestringToInt(string x)// Reverse output Int Type result
{
int tmp1 = 0;
int result = 0;
for (int i = 0; i < x.size(); ++i)
{
tmp1 = int(x.c_str()[i]) - 48;
if(i == 9 && tmp1>=2 && result>147483647)// Judge the critical point (int Maximum 2147483647)
{
return 0;
}
result += tmp1 * pow(10, i);
}
return result;
}
int reverse(int x) {
string tmp = intToString(x);
bool flag = false;
if (tmp[0] == '-')// Exclude symbols when reversing
{
tmp = tmp.substr(1);
flag = true;
}
int iRet = RestringToInt(tmp);
if(flag)// take ‘-’ Make up
{
iRet = iRet * (-1);
}
return iRet;
}
};
边栏推荐
- 什么是时间复杂度
- SQL injection less42 (post stack injection)
- 40. Combined sum II
- [leetcode] maximum depth of binary tree
- The applet listens for the target node to appear on the screen
- vuejs中如何实现动态路由切换及路由的缓存
- 成立不到一年!MIT衍生量子计算公司完成900万美元融资
- HCIP(11)
- How to realize dynamic route switching and route caching in vuejs
- array_ diff_ The method of not comparing array values when Assoc element is an array
猜你喜欢

HCIP(15)

腾讯云数据库负责人借了一亿元炒股?知情人士:金额不实

Learning notes and summary of C language programming specification

The person in charge of Tencent cloud database borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true

Future trend of defi in bear market

罗克韦尔AB PLC RSLogix数字量IO模块基本介绍

Hcip experiment (14)

HCIP(14)

Win11怎么打开软件通知

SQL injection less42 (post stack injection)
随机推荐
Establishment of Ruiji takeout development environment
HCIP(8)
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
What testing services do third-party software testing institutions provide? Charging standard of software test report
成立不到一年!MIT衍生量子计算公司完成900万美元融资
【CVPR 2021】Cylinder3D:用于LiDAR点云分割的圆柱体非对称3D卷积网络
SQL injection less38 (Stack Injection)
使用webWorker执行后台任务
阿里云CDN实践
lotus 1.16.0 延长扇区过期时间
SQL injection less34 (post wide byte injection + Boolean blind injection)
Differences of display values
腾讯云数据库负责人林晓斌借一亿元炒股?知情人士:金额不实
HCIP(14)
JMeter installs third-party plug-ins plugins Manager
HCIP(13)
2021年数学建模B组代码
拥抱开源指南
vuejs中如何实现动态路由切换及路由的缓存
What is time complexity