当前位置:网站首页>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
}
}
边栏推荐
- 醒悟的日子,我是怎么一步一步走向软件测试的道路
- md5工具类
- Flask 上下文详解
- 2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { fmt.Pri
- Redis sentinel simply looks at the trade-offs between distributed high availability and consistency
- php短视频源码,点赞时会有大拇指动画飘起
- 通过Go语言创建CA与签发证书
- Introduction and application of bigfilter global transaction anti duplication component
- 攻防世界 misc 进阶区 2017_Dating_in_Singapore
- 攻防世界 MISC 进阶区 3-11
猜你喜欢
sobel过滤器
Business is too busy. Is there really no reason to have time for automation?
Introducing QA into the software development lifecycle is the best practice that engineers should follow
质量体系建设之路的分分合合
Redis sentinel simply looks at the trade-offs between distributed high availability and consistency
Hit the core in the advanced area of misc in the attack and defense world
业务太忙,真的是没时间搞自动化理由吗?
攻防世界 MISC 进阶区 hong
攻防世界 MISC 进阶区 can_has_stdio?
Wake up day, how do I step by step towards the road of software testing
随机推荐
剑指Offer 68 - II. 二叉树的最近公共祖先
md5工具类
Attack and defense world misc advanced area Hong
Detailed explanation of heap sort code
It is said that software testing is very simple, but why are there so many dissuasions?
sobel过滤器
php短视频源码,点赞时会有大拇指动画飘起
Jvm-Sandbox-Repeater的部署
SPSS installation and activation tutorial (including network disk link)
[Lua] Int64 support
9 - class
The proofreading activity of data science on the command line second edition was restarted
Easy to use app recommendation: scan QR code, scan barcode and view history
高中物理:直线运动
Redis sentinel simply looks at the trade-offs between distributed high availability and consistency
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
集群的概述与定义,一看就会
Sqlserver encrypts and decrypts data
LOGO特训营 第二节 文字与图形的搭配关系
MYSQL架构——用户权限与管理