当前位置:网站首页>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;
}
};
边栏推荐
- What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
- RFID仓库管理系统解决方案有哪些功能模块
- 阿里测试师用UI自动化测试实现元素定位
- mysql语句执行详解
- 实践示例理解js强缓存协商缓存
- Redis分布式锁的实现
- 看腾讯大老如何做接口自动化测试
- BFC interview Brief
- Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
- LeetCode+ 81 - 85 单调栈专题
猜你喜欢
What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
剑指 Offer II 80-100(持续更新)
一文搞懂Go语言中文件的读写与创建
[in-depth learning] review pytoch's 19 loss functions
九齐NY8B062D MCU规格书/datasheet
How does the computer save web pages to the desktop for use
Flet教程之 08 AppBar工具栏基础入门(教程含源码)
RFID仓储管理系统解决方案的优点
FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
随机推荐
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
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
Go notes (3) usage of go language FMT package
黄金k线图中的三角形有几种?
GVM使用
Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
Win11无法将值写入注册表项如何解决?
How does win11 search for wireless displays? Win11 method of finding wireless display device
What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
BFC interview Brief
测试员的算法面试题-找众数
Flet教程之 07 PopupMenuButton基础入门(教程含源码)
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
接口设计时的一些建议
哈希(Hash)竞猜游戏系统开发功能分析及源码
Idea restore default shortcut key
go语言笔记(4)go常用管理命令