当前位置:网站首页>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;
}
边栏推荐
- 130. Surrounding area
- Image data preprocessing
- Common configurations in rectangular coordinate system
- Basic realization of line graph
- A network composed of three convolution layers completes the image classification task of cifar10 data set
- Vs code configuration latex environment nanny level configuration tutorial (dual system)
- [note] common combined filter circuit
- Ag9311maq design 100W USB type C docking station data | ag9311maq is used for 100W USB type C to HDMI with PD fast charging +u3+sd/cf docking station scheme description
- Su embedded training - Day7
- C# ?,?.,?? .....
猜你喜欢
130. Zones environnantes
9.卷积神经网络介绍
11. Recurrent neural network RNN
Basic realization of line chart (II)
Cs5261type-c to HDMI alternative ag9310 | ag9310 alternative
5. Over fitting, dropout, regularization
Share a latex online editor | with latex common templates
Invalid V-for traversal element style
Password recovery vulnerability of foreign public testing
Binder core API
随机推荐
130. Surrounding area
130. Zones environnantes
国内首次,3位清华姚班本科生斩获STOC最佳学生论文奖
133. 克隆图
130. 被圍繞的區域
14. Draw network model structure
C# ?,?.,?? .....
Basic realization of line graph
Vscode reading Notepad Chinese display garbled code
Binder core API
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
5.过拟合,dropout,正则化
AI遮天传 ML-初识决策树
Y59. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (III, II)
7.正则化应用
Chapter VIII integrated learning
Su embedded training - Day7
The whole life cycle of commodity design can be included in the scope of industrial Internet
German prime minister says Ukraine will not receive "NATO style" security guarantee
Malware detection method based on convolutional neural network