当前位置:网站首页>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;
}
边栏推荐
- STL--String类的常用功能复写
- Chapter XI feature selection
- Smart grid overview
- Class head up rate detection based on face recognition
- The whole life cycle of commodity design can be included in the scope of industrial Internet
- 130. 被围绕的区域
- Semantic segmentation model base segmentation_ models_ Detailed introduction to pytorch
- Invalid V-for traversal element style
- My best game based on wechat applet development
- Vscode is added to the right-click function menu
猜你喜欢
Y59. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (III, II)
图像数据预处理
Vscode reading Notepad Chinese display garbled code
英雄联盟胜负预测--简易肯德基上校
General configuration tooltip
Fofa attack and defense challenge record
Complete model training routine
2022-07-07: the original array is a monotonic array with numbers greater than 0 and less than or equal to K. there may be equal numbers in it, and the overall trend is increasing. However, the number
Smart grid overview
Scheme selection and scheme design of multifunctional docking station for type C to VGA HDMI audio and video launched by ange in Taiwan | scheme selection and scheme explanation of usb-c to VGA HDMI c
随机推荐
How to write mark down on vscode
Cs5261type-c to HDMI alternative ag9310 | ag9310 alternative
Swift get URL parameters
8.优化器
Saving and reading of network model
Introduction to ML regression analysis of AI zhetianchuan
New library online | cnopendata China Star Hotel data
Serial port receives a packet of data
Study notes of single chip microcomputer and embedded system
Smart agricultural technology framework
Chapter 16 intensive learning
Ag9310 for type-C docking station scheme circuit design method | ag9310 for type-C audio and video converter scheme circuit design reference
Ag7120 and ag7220 explain the driving scheme of HDMI signal extension amplifier | ag7120 and ag7220 design HDMI signal extension amplifier circuit reference
Semantic segmentation model base segmentation_ models_ Detailed introduction to pytorch
手机上炒股安全么?
swift获取url参数
Vscode is added to the right-click function menu
Su embedded training - Day5
Stock account opening is free of charge. Is it safe to open an account on your mobile phone
USB type-C docking design | design USB type-C docking scheme | USB type-C docking circuit reference