当前位置:网站首页>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;
}边栏推荐
- Uncaught typeerror: cannot read properties of null (reading 'append') solution
- [leetcode weekly replay] 303rd weekly 20220724
- EF core :自引用的组织结构树
- Dynamic programming-01 knapsack rolling array optimization
- 2. Load test
- 云计算三类巨头:IaaS、PaaS、SaaS,分别是什么意思,应用场景是什么?
- Does opengauss support using Sqlalchemy connections?
- UART
- Why do I have to clean up data?
- The use of where condition in MySQL is not equal to! = The problem that null values are filtered out occurs when in, etc
猜你喜欢

Implement a avatar looping control

QT project - security monitoring system (function realization of each interface)

每周小结(*66):下一个五年

如果实现与在线CAD图中的线段实时求交点

Qt学习-利用数据库单例完成 登录匹配 + 注册 功能实现

How to put long links into Excel

Use es to realize fuzzy search and search recommendation of personal blog

采坑记录:TypeError: 'module' object is not callable

来自大佬洗礼!2022 头条首发纯手打 MySQL 高级进阶笔记, 吃透 P7 有望
![[leetcode weekly replay] 303rd weekly 20220724](/img/ba/0f16f1f42e4a2593ec0124f23b30d7.png)
[leetcode weekly replay] 303rd weekly 20220724
随机推荐
Modify the existing annotator name in the word document
Transmission download list, download file migration machine guide
[untitled]
Redis memory analysis tool RMA usage
Advanced function of postman
The new version of SSM video tutorial in shangsilicon valley was released
[mindspore] [xception model] script statement is suspected to be wrong
@Mapkey usage instructions
[help] mindspire training based on ascend910 cannot reproduce the model effect on GPU
[acwing周赛复盘] 第 61 场周赛20220723
Shell echo command
Live broadcast preview | online seminar on open source security governance models and tools
Paper time review MB2: build a behavior model for autonomous databases
Leetcode 0125. validate palindrome string
Heap and stack in embedded development
QT learning - using database singleton to complete login matching + registration function
Principle of data proxy
[acwing weekly rematch] 61st weekly 20220723
What is the function of transdata operator and whether it can optimize performance
C语言学习之分支与循环语句