当前位置:网站首页>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;
}
边栏推荐
- [note] common combined filter circuit
- Is it safe to speculate in stocks on mobile phones?
- Chapter IV decision tree
- 8. Optimizer
- 14. Draw network model structure
- Using GPU to train network model
- AI zhetianchuan ml novice decision tree
- 第四期SFO销毁,Starfish OS如何对SFO价值赋能?
- 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
猜你喜欢
9.卷积神经网络介绍
Basic implementation of pie chart
Taiwan Xinchuang sss1700 latest Chinese specification | sss1700 latest Chinese specification | sss1700datasheet Chinese explanation
Cross modal semantic association alignment retrieval - image text matching
Parade ps8625 | replace ps8625 | EDP to LVDS screen adapter or screen drive board
Chapter VIII integrated learning
Invalid V-for traversal element style
Generic configuration legend
How to transfer Netease cloud music /qq music to Apple Music
Know how to get the traffic password
随机推荐
Know how to get the traffic password
Serial port receives a packet of data
1.线性回归
Prediction of the victory or defeat of the League of heroes -- simple KFC Colonel
Mathematical modeling -- knowledge map
My best game based on wechat applet development
新库上线 | 中国记者信息数据
y59.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三二)
Chapter 16 intensive learning
8. Optimizer
Ag9310 design USB type C to hdmi+u2+5v slow charging scheme design | ag9310 expansion dock scheme circuit | type-C dongle design data
8.优化器
11.递归神经网络RNN
完整的模型验证(测试,demo)套路
4.交叉熵
letcode43:字符串相乘
Complete model verification (test, demo) routine
Vscode is added to the right-click function menu
USB type-C mobile phone projection scheme | USB type-C docking station scheme | TV / projector type-C converter scheme | ag9300ag9310ag9320
How to transfer Netease cloud music /qq music to Apple Music