当前位置:网站首页>C language force buckle the flipped number of question 7. Violence Act
C language force buckle the flipped number of question 7. Violence Act
2022-07-25 00:15:00 【Take care of two dogs and never let them go bad】
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
source : Power button (LeetCode)
link :https ://leetcode.cn/problems/reverse-integer
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
If overflow is not considered in this problem , It's very simple . There are two ways to solve the overflow problem , The first idea is to add... Through string conversion try catch To solve the problem , The second idea is to solve .
Because the efficiency of string conversion is low and more library functions are used , Therefore, this method is not considered in the problem-solving scheme , But through mathematical calculations to solve .
By looping numbers x Every one of them took apart , When calculating a new value, each step determines whether it overflows .
There are two overflow conditions , One is greater than the integer maximum MAX_VALUE, The other is less than the integer minimum MIN_VALUE, Let the current calculation result be ans, The next one is pop.
from ans * 10 + pop > MAX_VALUE In terms of this overflow condition
When there is a ans > MAX_VALUE / 10 And also pop Need to add when , It must overflow
When there is a ans == MAX_VALUE / 10 And pop > 7 when , It must overflow ,7 yes 2 ^ 31 - 1 The number of digits
from ans * 10 + pop < MIN_VALUE In terms of this overflow condition
When there is a ans < MIN_VALUE / 10 And also pop Need to add when , It must overflow
When there is a ans == MIN_VALUE / 10 And pop < -8 when , It must overflow ,8 yes - 2 ^ 31 The number of digits
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int test(int x)
{
int y = 0;
int z = 0;
int max = 2147483647;//int Maximum
int min = -max - 1;
//printf("%d\n", -max-1);
//printf("%d\n", max);
if (x == 0)
{
return x;
}
if (x == min)// because x Negative is one more than positive It may cause that negative numbers cannot be de negative List the last negative number
{
return 0;
}
z = x;// Start with the first x Stored in the z in In order to facilitate the subsequent classification discussion
while (x != 0)
{
if (x < 0)//x Less than 0 The situation of
x = -x;
//printf("%d\n", x);
y = y * 10 + x % 10;
//printf("%d\n", y);
x = x / 10;
if (x/10 == 0 && x != 0)// Check for spillage
{
if (z > 0)
{
if (y > max / 10 || ( y == max/10 && x > max % 10))
return 0;
}
if (z < 0)
{
if ((-y) < (-max - 1)/10 || ((-y)== (-max - 1) / 10 && (-x)< (-max - 1) % 10))
return 0;
}
}
}
if (z < 0)
y = -y;
return y;
}
int main()
{
int x = -2147483647;
x = test(x);
printf("%d", x);
return 0;
}边栏推荐
- Simple operation K6
- 来自大佬洗礼!2022 头条首发纯手打 MySQL 高级进阶笔记, 吃透 P7 有望
- Opengauss kernel analysis: query rewriting
- Where are MySQL version numbers 6 and 7?
- [英雄星球七月集训LeetCode解题日报] 第24日 线段树
- Lambda&Stream
- The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
- [leetcode weekly replay] 303rd weekly 20220724
- 在混合云中管理数据库:八个关键注意事项
- Exception, import package and file operation
猜你喜欢
![[LeetCode周赛复盘] 第 83 场双周赛20220723](/img/db/c264c94ca3307d4363d3cf7f5d770b.png)
[LeetCode周赛复盘] 第 83 场双周赛20220723
![[help] mindspire training based on ascend910 cannot reproduce the model effect on GPU](/img/b5/c02ef57526d208b02dfa00189e9ecb.jpg)
[help] mindspire training based on ascend910 cannot reproduce the model effect on GPU

Nodejs package

UART

Quartus: install cyclone 10 LP device library for quartus version 17.1

What is the function of transdata operator and whether it can optimize performance

50 places are limited to open | with the news of oceanbase's annual press conference coming!

Sql文件导入数据库-保姆级教程
![[acwing weekly rematch] 61st weekly 20220723](/img/8b/df2c8d516db1e7e5f2d50bcf62b2b1.png)
[acwing weekly rematch] 61st weekly 20220723

Grafana - influxdb visual K6 output
随机推荐
LeetCode_6124_第一个出现两次的字母
R language uses ISNA function to check whether the list and dataframe contain missing values, marks abnormal values in data columns in dataframe as missing values Na, and uses na.omit function to dele
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
采坑记录:TypeError: 'module' object is not callable
[英雄星球七月集训LeetCode解题日报] 第24日 线段树
LeetCode_ 392_ Judgement subsequence
每周小结(*66):下一个五年
Exception, import package and file operation
Internal network mapping port to external network
[leetcode weekly replay] game 83 biweekly 20220723
Principle of data proxy
Redis6.2 SYSTEMd startup prompt redis service: Failed with result ‘protocol‘.
[help] mindspire training based on ascend910 cannot reproduce the model effect on GPU
NXP i.mx6q development board software and hardware are all open source, and the schematic diagram of the core board is provided
What is the function of transdata operator and whether it can optimize performance
云图
Wechat applet development learning 5 (custom components)
来自大佬洗礼!2022 头条首发纯手打 MySQL 高级进阶笔记, 吃透 P7 有望
Modify the existing annotator name in the word document
Discussion on line segment tree