当前位置:网站首页>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
}
}
边栏推荐
- 虚拟人产业面临的挑战
- 【室友用一局王者荣耀的时间学会了用BI报表数据处理】
- Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
- Locust performance test - environment construction and use
- MySQL storage data encryption
- LOGO特訓營 第三節 首字母創意手法
- Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
- SQL中MAX与GREATEST的区别
- Challenges faced by virtual human industry
- Test will: bug classification and promotion solution
猜你喜欢

攻防世界 MISC 进阶区 3-11

Logo special training camp Section IV importance of font design

SPSS安装激活教程(包含网盘链接)

醒悟的日子,我是怎么一步一步走向软件测试的道路

安装人大金仓数据库

堆排序代码详解

Why is Dameng data called the "first share" of domestic databases?

【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用

Attack and defense world misc advanced area can_ has_ stdio?

新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
随机推荐
Short video system source code, click the blank space of the screen, the keyboard does not automatically stow
Test will: bug classification and promotion solution
Mongodb aggregation operation summary
攻防世界 MISC 进阶区 hit-the-core
Solana chain application crema was shut down due to hacker attacks
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
都说软件测试很简单有手就行,但为何仍有这么多劝退的?
MySQL storage data encryption
业务太忙,真的是没时间搞自动化理由吗?
国产数据库乱象
MYSQL架构——逻辑架构
攻防世界 MISC 进阶区 Ditf
LOGO特训营 第五节 字体结构与设计常用技法
堆排序代码详解
LOGO特训营 第三节 首字母创意手法
Recommendation of mobile app for making barcode
Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code
Practice and principle of PostgreSQL join
攻防世界 misc 高手进阶区 a_good_idea
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse