当前位置:网站首页>LeetCode 8. String conversion integer (ATOI)
LeetCode 8. String conversion integer (ATOI)
2022-07-04 20:56:00 【_ Liu Xiaoyu】
Please come to realize a myAtoi(string s) function , Enable it to convert a string to a 32 Bit signed integer ( similar C/C++ Medium atoi function ).
function myAtoi(string s) The algorithm is as follows :
Read in strings and discard useless leading spaces
Check the next character ( Suppose you haven't reached the end of the character yet ) Positive or negative , Read the character ( If there is ). Determine whether the final result is negative or positive . If neither exists , Suppose the result is positive .
Read in the next character , Until you reach the next non numeric character or the end of the input . The rest of the string will be ignored .
Convert the numbers read in the previous steps into integers ( namely ,“123” -> 123, “0032” -> 32). If you don't read in the numbers , Then the integer is 0 . Change the symbol if necessary ( From step 2 Start ).
If the number of integers exceeds 32 Bit signed integer range [−231, 231 − 1] , You need to truncate this integer , Keep it in this range . say concretely , Less than −231 The integer of should be fixed to −231 , Greater than 231 − 1 The integer of should be fixed to 231 − 1 .
Returns an integer as the final result .
Be careful :
The white space character in this question only includes the space character ’ ’ .
Except for the leading space or the rest of the string after the number , Do not ignore Any other character .
Example 1:
Input :s = “42”
Output :42
explain : The bold string is the character that has been read in , The caret is the character currently read .
The first 1 Step :“42”( No characters are currently read in , Because there are no leading spaces )
^
The first 2 Step :“42”( No characters are currently read in , Because it doesn't exist here ‘-’ perhaps ‘+’)
^
The first 3 Step :“42”( Read in “42”)
^
Parse to get an integer 42 .
because “42” In scope [-231, 231 - 1] Inside , The final result is 42 .
Example 2:
Input :s = " -42"
Output :-42
explain :
The first 1 Step :" -42"( Read in leading space , But ignore )
^
The first 2 Step :" -42"( Read in ‘-’ character , So the result should be negative )
^
The first 3 Step :" -42"( Read in “42”)
^
Parse to get an integer -42 .
because “-42” In scope [-231, 231 - 1] Inside , The final result is -42 .
Example 3:
Input :s = “4193 with words”
Output :4193
explain :
The first 1 Step :“4193 with words”( No characters are currently read in , Because there are no leading spaces )
^
The first 2 Step :“4193 with words”( No characters are currently read in , Because it doesn't exist here ‘-’ perhaps ‘+’)
^
The first 3 Step :“4193 with words”( Read in “4193”; Because the next character is not a number , So read in stop )
^
Parse to get an integer 4193 .
because “4193” In scope [-231, 231 - 1] Inside , The final result is 4193 .
Code:
class Solution {
public:
int myAtoi(string s) {
int re = 0;
int k = 0;
while(k < s.size() && s[k] == ' ') k ++;
if( k == s.size()) return 0;
int flag = 1;
if(s[k] == '-') flag = -1, k ++;
if(s[k] == '+')
{
if (flag == -1) return 0;
else
k ++;
}
while(k < s.size() && s[k] >='0' && s[k] <= '9')
{
int x = s[k] - '0';
if(flag > 0 && re > (INT_MAX - x)/ 10) return INT_MAX;
if(flag < 0 && -re < (INT_MIN + x)/ 10) return INT_MIN;
if(-re * 10 - x == INT_MIN) return INT_MIN; // This sentence is a special judgment , INT_MN and INT_MAX The absolute value of is different
re = re * 10 + x;
k++;
}
re *= flag;
return re;
}
};
边栏推荐
猜你喜欢
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
Flet教程之 06 TextButton基础入门(教程含源码)
QT writing the Internet of things management platform 38- multiple database support
LeetCode+ 81 - 85 单调栈专题
强化学习-学习笔记2 | 价值学习
NetCore3.1 Json web token 中间件
Selected review | machine learning technology for Cataract Classification / classification
Flet教程之 05 OutlinedButton基础入门(教程含源码)
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
Win11亮度被锁定怎么办?Win11亮度被锁定的解决方法
随机推荐
acwing 3302. 表达式求值
Flet教程之 07 PopupMenuButton基础入门(教程含源码)
接口設計時的一些建議
Go notes (3) usage of go language FMT package
How to adapt your games to different sizes of mobile screen
Record the online bug solving list (unfinished to be continued 7/4)
Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
go笔记(3)Go语言fmt包的用法
工厂从自动化到数字孪生,图扑能干什么?
【深度学习】一文看尽Pytorch之十九种损失函数
After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
Flet tutorial 07 basic introduction to popupmenubutton (tutorial includes source code)
Idea configuration standard notes
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
托管式服务网络:云原生时代的应用体系架构进化
So this is the BGP agreement
E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
哈希(Hash)竞猜游戏系统开发功能分析及源码
看腾讯大老如何做接口自动化测试
Jiuqi ny8b062d MCU specification /datasheet