当前位置:网站首页>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;
}
};
边栏推荐
- ACM组合计数入门
- Go language notes (4) go common management commands
- AP8022开关电源小家电ACDC芯片离线式开关电源IC
- 长城证券开户安全吗 股票开户流程网上开户
- Cdga | six principles that data governance has to adhere to
- Flet教程之 06 TextButton基础入门(教程含源码)
- GVM use
- 电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
- How does win11 search for wireless displays? Win11 method of finding wireless display device
- 奏响青春的乐章
猜你喜欢
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
MySQL中的日期时间类型与格式化方式
伦敦银走势图分析的新方法
Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
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
So this is the BGP agreement
Common verification rules of form components -1 (continuously updating ~)
Flet教程之 08 AppBar工具栏基础入门(教程含源码)
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
随机推荐
实操自动生成接口自动化测试用例
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
记一次重复造轮子(Obsidian 插件设置说明汉化)
node强缓存和协商缓存实战示例
语义化标签的优势和块级行内元素
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
长城证券开户安全吗 股票开户流程网上开户
记一次重复造轮子(Obsidian 插件设置说明汉化)
Selected review | machine learning technology for Cataract Classification / classification
【解决方案】PaddlePaddle 2.x调用静态图模式
Flet教程之 06 TextButton基础入门(教程含源码)
剑指 Offer II 80-100(持续更新)
PermissionError: [Errno 13] Permission denied: ‘data.csv‘
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
What ppt writing skills does the classic "pyramid principle" teach us?
What is involution?
九齐NY8B062D MCU规格书/datasheet
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
ACM组合计数入门
Integretee integrates into Moonriver through xcm, bringing enterprise class privacy solutions to its ecosystem