当前位置:网站首页>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
}
}
边栏推荐
- Business is too busy. Is there really no reason to have time for automation?
- 【室友用一局王者荣耀的时间学会了用BI报表数据处理】
- MYSQL架构——逻辑架构
- Jvm-Sandbox-Repeater的部署
- Easy to use app recommendation: scan QR code, scan barcode and view history
- 堆排序代码详解
- 测试必会:BUG的分类及推进解决
- UML图记忆技巧
- Deployment of JVM sandbox repeater
- About stack area, heap area, global area, text constant area and program code area
猜你喜欢
Erik baleog and Olaf, advanced area of misc in the attack and defense world
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
Attack and defense world misc advanced grace-50
攻防世界 MISC 进阶 glance-50
攻防世界 MISC 进阶区 Ditf
Introduction and application of bigfilter global transaction anti duplication component
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
Attack and defense world misc advanced area ditf
集群的概述与定义,一看就会
Wake up day, how do I step by step towards the road of software testing
随机推荐
MySQL Architecture - logical architecture
面试必备 LeetCode 链表算法题汇总,全程干货!
Analog rocker controlled steering gear
Li Kou 98: verify binary search tree
Analysis of environmental encryption technology
啃下大骨头——排序(二)
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
Tla+ introductory tutorial (1): introduction to formal methods
How to reset the password of MySQL root account
Sqlserver encrypts and decrypts data
LOGO特訓營 第三節 首字母創意手法
安装人大金仓数据库
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
攻防世界 MISC 进阶区 hong
Logo special training camp section II collocation relationship between words and graphics
NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
攻防世界 MISC 进阶区 can_has_stdio?
2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { fmt.Pri
Unity vscode emmylua configuration error resolution