当前位置:网站首页>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 tutorial 06 basic introduction to textbutton (tutorial includes source code)
- 2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
- CDGA|数据治理不得不坚持的六个原则
- Why is TCP three handshakes and four waves
- Après l'insertion de l'image dans le mot, il y a une ligne vide au - dessus de l'image, et la disposition est désordonnée après la suppression
- BFC interview Brief
- What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
- Practical examples of node strong cache and negotiation cache
- Idea configuration standard notes
- Hands on deep learning (III) -- convolutional neural network CNN
猜你喜欢
![[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt](/img/0d/aa7f82fada743ea2ec23355ef954df.jpg)
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt

面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧

Flet tutorial 05 outlinedbutton basic introduction (tutorial includes source code)

Selected review | machine learning technology for Cataract Classification / classification

Flet教程之 05 OutlinedButton基础入门(教程含源码)

FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
Practice examples to understand JS strong cache negotiation cache

In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills

What if win11u disk refuses access? An effective solution to win11u disk access denial

Form组件常用校验规则-1(持续更新中~)
随机推荐
黄金k线图中的三角形有几种?
Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t
AP8022开关电源小家电ACDC芯片离线式开关电源IC
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
Why is the maximum speed the speed of light
强化学习-学习笔记2 | 价值学习
Flet tutorial 05 outlinedbutton basic introduction (tutorial includes source code)
[in-depth learning] review pytoch's 19 loss functions
剑指 Offer II 80-100(持续更新)
Après l'insertion de l'image dans le mot, il y a une ligne vide au - dessus de l'image, et la disposition est désordonnée après la suppression
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
记一次重复造轮子(Obsidian 插件设置说明汉化)
一文搞懂Go语言中文件的读写与创建
jekins初始化密码没有或找不到
After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
What if win11u disk refuses access? An effective solution to win11u disk access denial
Taishan Office Technology Lecture: about the order of background (shading and highlighting)
Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
Reinforcement learning - learning notes 2 | value learning