当前位置:网站首页>Sword finger offer 67 Convert a string to an integer
Sword finger offer 67 Convert a string to an integer
2022-07-04 22:45:00 【LuZhouShiLi】
The finger of the sword Offer 67. Convert a string to an integer
subject
Write a function StrToInt, Realize the function of converting string to integer . Out of commission atoi Or other similar library functions .
First , This function discards the useless start space characters as needed , Until the first non space character is found .
When the first non empty character we find is a positive or negative sign , Then combine the symbol with as many consecutive numbers as possible on the back face , As the sign of the integer ; If the first non empty character is a number , Then combine it directly with the following consecutive numeric characters , Form an integer .
In addition to the valid integer part of the string, there may be extra characters , These characters can be ignored , They should have no effect on functions .
Be careful : If the first non space character in the string is not a valid integer character 、 When the string is empty or the string contains only white space characters , Then your function doesn't need to be converted .
In any case , If the function cannot be effectively converted , Please return 0.
Ideas
- First delete the space character
- Sign bit : Create a new variable to save the symbol bit ,‘-’ The sign bit is recorded as 1
- Nonnumeric character : Encountered the first non numeric character , Go back to
- Numeric character :
- Character to number : Subtract... From this character ’0’ that will do
- Digital stitching : Set the current bit character to c, The current digit is x, The numerical result is res, Then the number splicing formula is :res = res * 10 + c - ‘0’
Code
class Solution {
public int strToInt(String str) {
char[] c = str.trim().toCharArray();// Remove space
if(c.length == 0) return 0;
int res = 0;
int bndry = Integer.MAX_VALUE / 10;
int i = 1,sign = 1;
if(c[0] == '-') sign = -1; // Sign bit
else if(c[0] != '+') i = 0;
for(int j = i; j < c.length; j++)
{
if(c[j] < '0' || c[j] > '9')
{
break;// Not a digit Jump straight out of the loop Then go straight back
}
// Transboundary
if(res > bndry || res == bndry && c[j] > '7') return sign == 1 ? Integer.MAX_VALUE:Integer.MIN_VALUE;
// Add up
res = res * 10 + (c[j] - '0');
}
return sign * res;// Record symbol bit
}
}
边栏推荐
- Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
- 特征缩放 标准化 归一化
- [cooking record] - stir fried 1000 pieces of green pepper
- MYSQL架构——逻辑架构
- Concurrent optimization summary
- Deployment of JVM sandbox repeater
- 高中物理:直线运动
- Microservices -- Opening
- 模拟摇杆控制舵机
- MySQL storage data encryption
猜你喜欢
Locust performance test - environment construction and use
Serial port data frame
攻防世界 MISC 高手进阶区 001 normal_png
国产数据库乱象
Mongodb aggregation operation summary
攻防世界 MISC 进阶区 can_has_stdio?
Logo Camp d'entraînement section 3 techniques créatives initiales
Business is too busy. Is there really no reason to have time for automation?
达梦数据凭什么被称为国产数据库“第一股”?
攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
随机推荐
LOGO特訓營 第三節 首字母創意手法
Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
Solana chain application crema was shut down due to hacker attacks
md5工具类
sobel过滤器
LOGO special training camp section I identification logo and Logo Design Ideas
Domestic database chaos
SQL中MAX与GREATEST的区别
攻防世界 MISC 高手进阶区 001 normal_png
How to send a reliable request before closing the page
Advanced area a of attack and defense world misc Masters_ good_ idea
剑指 Offer 65. 不用加减乘除做加法
Attack and defense world misc advanced area ditf
SPSS installation and activation tutorial (including network disk link)
UML diagram memory skills
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
虚拟人产业面临的挑战
攻防世界 MISC 进阶 glance-50
页面关闭前,如何发送一个可靠请求