当前位置:网站首页>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
}
}
边栏推荐
- Unity vscode emmylua configuration error resolution
- Summary of index operations in mongodb
- Microservices -- Opening
- Postgresqlql advanced skills pivot table
- Challenges faced by virtual human industry
- Logo special training camp Section IV importance of font design
- Shell script implements application service log warehousing MySQL
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
- Jvm-Sandbox-Repeater的部署
- 页面关闭前,如何发送一个可靠请求
猜你喜欢

Logo special training camp section 1 Identification logo and logo design ideas

Advanced area of attack and defense world misc 3-11

Serial port data frame

Domestic database chaos
页面关闭前,如何发送一个可靠请求

LOGO特训营 第三节 首字母创意手法

NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
How to send a reliable request before closing the page

攻防世界 misc 高手进阶区 a_good_idea

攻防世界 MISC 进阶区 Ditf
随机推荐
How to manage 15million employees easily?
Easy to use app recommendation: scan QR code, scan barcode and view history
Unity-VScode-Emmylua配置报错解决
繁华落尽、物是人非:个人站长该何去何从
About stack area, heap area, global area, text constant area and program code area
MySQL Architecture - user rights and management
Advanced area a of attack and defense world misc Masters_ good_ idea
国产数据库乱象
The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
LOGO特訓營 第三節 首字母創意手法
都说软件测试很简单有手就行,但为何仍有这么多劝退的?
攻防世界 misc 高手进阶区 a_good_idea
Concurrent network modular reading notes transfer
Mongodb aggregation operation summary
不同环境相同配置项的内容如何diff差异?
堆排序代码详解
质量体系建设之路的分分合合
It is said that software testing is very simple, but why are there so many dissuasions?
安装人大金仓数据库