当前位置:网站首页>Leetcode notes No.7
Leetcode notes No.7
2022-07-08 01:13:00 【__ Small crisp__】
7. Integer inversion
Give a 32 Signed integer of bit , You need to reverse the number on each of the integers .
Be careful
Suppose our environment can only store 32 Signed integer of bit , The value range is [−231, 231 − 1]. Please follow this assumption , If the integer overflows after inversion, return 0.
Ideas
- Through strings , However, a large number of library functions are required
- Mathematical methods
- 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 INT_MAX, The other is less than the integer minimum INT_MIN, Let the current calculation result be result, The next one is temp.
- from result* 10 + temp> INT_MAX In terms of this overflow condition
- When there is a result > INT_MAX / 10 And also temp Need to add when , It must overflow
- When there is a result == INT_MAX / 10 And temp> 7 when , It must overflow ,7 yes 2^31 - 1 The number of digits
- from result* 10 + temp < INT_MIN In terms of this overflow condition
- When there is a result < INT_MIN/ 10 And also temp Need to add when , It must overflow
- When there is a result == INT_MIN/ 10 And temp< -8 when , It must overflow ,8 yes -2^31 The number of digits
Code
int reverse(int x){
int temp = 0;
int result = 0;
while(x != 0) {
temp = x % 10;
if (result > INT_MAX / 10 || ((result == INT_MAX / 10) && temp > 7)) {
return 0;
}
if (result < INT_MIN / 10 || ((result == INT_MIN / 10) && temp < -8)) {
return 0;
}
result = 10 * result + temp;
x /= 10;
}
return result;
}
边栏推荐
- 【深度学习】AI一键换天
- Vscode reading Notepad Chinese display garbled code
- Smart agricultural technology framework
- 10. CNN applied to handwritten digit recognition
- 7.正则化应用
- Common effects of line chart
- 11.递归神经网络RNN
- Design method and application of ag9311maq and ag9311mcq in USB type-C docking station or converter
- 图像数据预处理
- A network composed of three convolution layers completes the image classification task of cifar10 data set
猜你喜欢
7.正则化应用
Using GPU to train network model
Basic realization of line chart (II)
7. Regularization application
解决报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
130. 被围绕的区域
11. Recurrent neural network RNN
AI遮天传 ML-初识决策树
1. Linear regression
Redis, do you understand the list
随机推荐
50MHz generation time
Cs5212an design display to VGA HD adapter products | display to VGA Hd 1080p adapter products
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
Overall introduction of the project
Binder core API
13. Model saving and loading
Cross modal semantic association alignment retrieval - image text matching
My best game based on wechat applet development
Complete model verification (test, demo) routine
Mathematical modeling -- knowledge map
Cs5261type-c to HDMI alternative ag9310 | ag9310 alternative
1.线性回归
General configuration toolbox
Semantic segmentation model base segmentation_ models_ Detailed introduction to pytorch
Led serial communication
Definition and classification of energy
Chapter VIII integrated learning
Smart agricultural technology framework
STL--String类的常用功能复写
Ag9310 same function alternative | cs5261 replaces ag9310type-c to HDMI single switch screen alternative | low BOM replaces ag9310 design