当前位置:网站首页>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;
}边栏推荐
- [英雄星球七月集训LeetCode解题日报] 第24日 线段树
- Leetcode 1260. two dimensional grid migration: two solutions (k simulations / one step)
- ROS manipulator movelt learning notes 3 | kinect360 camera (V1) related configuration
- Lambda&Stream
- Netease game Flink SQL platform practice
- What can testers do when there is an online bug?
- Install K6 test tool
- C language: deep analysis function stack frame
- How to make five kinds of data structures in redis
- 软考 --- 程序设计语言基础(下)
猜你喜欢

The model needs to use two losses_ FN, how to operate?

你还在使用System.currentTimeMillis()?来看看StopWatch吧

Pain and happiness -nio programming

Heap and stack in embedded development

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

Sql文件导入数据库-保姆级教程

NXP i.mx6q development board software and hardware are all open source, and the schematic diagram of the core board is provided

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

How painful is it to write unit tests? Can you do it

Paper time review MB2: build a behavior model for autonomous databases
随机推荐
Weekly summary (*66): next five years
[Nuxt 3] (十)运行时配置
Regular expression learning
Be an artistic test / development programmer and slowly change yourself
Discussion on line segment tree
云计算三类巨头:IaaS、PaaS、SaaS,分别是什么意思,应用场景是什么?
Uncaught typeerror: cannot read properties of null (reading 'append') solution
Analyzing the principle of DNS resolution in kubernetes cluster
GUI basic application
Remember the problem of using redisson to step on the pit once
Leetcode 0125. validate palindrome string
[nuxt 3] (x) runtime configuration
Leetcode 1260. two dimensional grid migration: two solutions (k simulations / one step)
[hero planet July training leetcode problem solving daily] 24th line segment tree
痛并快乐的-NIO编程
How to put long links into Excel
Managing databases in a hybrid cloud: eight key considerations
Nodejs package
Server intranet and Extranet
SQL rewriting Series 6: predicate derivation