当前位置:网站首页>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
}
}
边栏推荐
- leetcode 72. Edit distance edit distance (medium)
- Easy to use app recommendation: scan QR code, scan barcode and view history
- The overview and definition of clusters can be seen at a glance
- SQL中MAX与GREATEST的区别
- PostgreSQLSQL高级技巧透视表
- PMO: compare the sample efficiency of 25 molecular optimization methods
- Concurrent network modular reading notes transfer
- MYSQL架构——逻辑架构
- Logo Camp d'entraînement section 3 techniques créatives initiales
- Concurrent optimization summary
猜你喜欢
Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
Logo Camp d'entraînement section 3 techniques créatives initiales
【OpenGL】笔记二十九、抗锯齿(MSAA)
Logo special training camp section II collocation relationship between words and graphics
达梦数据凭什么被称为国产数据库“第一股”?
安装人大金仓数据库
UML diagram memory skills
Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code
随机推荐
Attack and defense world misc advanced area Hong
Solana chain application crema was shut down due to hacker attacks
Deployment of JVM sandbox repeater
Logo special training camp Section IV importance of font design
Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集
La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
Business is too busy. Is there really no reason to have time for automation?
Attack and defense world misc master advanced zone 001 normal_ png
LOGO特训营 第二节 文字与图形的搭配关系
业务太忙,真的是没时间搞自动化理由吗?
POM in idea XML dependency cannot be imported
Wake up day, how do I step by step towards the road of software testing
微服务--开篇
Shell script implements application service log warehousing MySQL
都说软件测试很简单有手就行,但为何仍有这么多劝退的?
idea中pom.xml依赖无法导入
Introduction and application of bigfilter global transaction anti duplication component
SPSS installation and activation tutorial (including network disk link)
Short video system source code, click the blank space of the screen, the keyboard does not automatically stow
In Linux, I call odspcmd to query the database information. How to output silently is to only output values. Don't do this