当前位置:网站首页>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;
}边栏推荐
- torch.nn.SyncBatchNorm.convert_ sync_ Mindspore usage corresponding to batchnorm
- How to put long links into Excel
- Leetcode 0125. validate palindrome string
- Quartus: install cyclone 10 LP device library for quartus version 17.1
- NFT chain game system development metauniverse gamefi construction
- Install K6 test tool
- 数组中只出现一次的两个数字
- 做一个文艺的测试/开发程序员,慢慢改变自己......
- Where are MySQL version numbers 6 and 7?
- c语言:深度刨析函数栈帧
猜你喜欢

Implement a avatar looping control

C language program environment and preprocessing

2022 Henan Mengxin League game 2: Henan University of technology K - Rice

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

C语言学习之分支与循环语句

数组中只出现一次的两个数字

SQL file import database - Nanny level tutorial

Development direction and problems of optaplanner

@Mapkey usage instructions

C language: deep analysis function stack frame
随机推荐
Two numbers that appear only once in the array
Unity+photon self made multiplayer TPS game
Paper time review MB2: build a behavior model for autonomous databases
Where are MySQL version numbers 6 and 7?
Quartus: install cyclone 10 LP device library for quartus version 17.1
Install software on kubernetes cluster using helm 3 package manager
Efficiency increased by 98%! AI weapon behind operation and maintenance inspection of high altitude photovoltaic power station
EF core :自引用的组织结构树
Transmission download list, download file migration machine guide
How to put long links into Excel
Soft test --- fundamentals of programming language (Part 2)
Shell echo command
【无标题】
Processing PDF and JPG files in VB6
Beisen prospectus: the advantages of the track are prominent, and integration + medium and large customers are plus points
2022 Henan Mengxin League game 2: Henan University of technology I - 22
c语言:深度刨析函数栈帧
First experience of flask
Does opengauss support using Sqlalchemy connections?
1. Smoke test