当前位置:网站首页>LeetCode 8. String to integer (ATOI) (medium, string)
LeetCode 8. String to integer (ATOI) (medium, string)
2022-06-11 00:50:00 【Grilled little fat sheep with charcoal...】
Title Description : 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 :
1. Read in strings and discard useless leading spaces
2. 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 .
3. 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 .
4. 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 .
5. 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 .
Tips :
0 <= s.length <= 200
s By the English letters ( Uppercase and lowercase )、 Numbers (0-9)、' '、'+'、'-' and '.' form
Code implementation :
public static int myAtoi(String s){
if(s.length() == 0){
return 0;
}
int index = 0; // Traverse to the current character position
long res = 0; // final result
int sign = 1; // 1 Is an integer , -1 It's a negative number
int length = s.length(); // The length of the array
// Remove spaces logically
while (index < length && s.charAt(index) == ' '){
index++;
}
if(index == length){
return 0;
}
// Judging symbols
if(s.charAt(index) == '-' || s.charAt(index) == '+'){
sign = s.charAt(index++) == '+' ? 1 : -1;
}
for (; index < length; index++) {
int digit = s.charAt(index) - '0';
if(digit < 0 || digit > 9){
// If it is not a number, exit directly
break;
}
res = res * 10 + digit;
// If the maximum value is exceeded , Return the maximum value directly
if(res * sign > Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}
// If it's less than the minimum , Return the minimum value directly
if(res * sign < Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}
}
return (int) (sign * res);
}
source : Power button (LeetCode)
link :https://leetcode.cn/problems/string-to-integer-atoi
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
边栏推荐
- Room第一次使用
- How to ensure the sequence of messages, that messages are not lost or consumed repeatedly
- Yum source update
- 图的最短路径问题 详细分解版
- Signature verification failed during system application installation
- 百度飞桨PaddlePaddle最新系列AI课程回放地址
- 阻塞隊列 — DelayedWorkQueue源碼分析
- 跳转页面后回不去默认页面
- MESI cache consistency protocol for concurrent programming
- qt程序插件报错plugin xcb
猜你喜欢

Pirate OJ 448 luck draw
![[network planning] 2.1.2 transport layer services that can be selected by the application](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network planning] 2.1.2 transport layer services that can be selected by the application

动态规划经典题目三角形最短路径

Word在目录里插入引导符(页码前的小点点)的方法

为什么使用 Golang 进行 Web 开发
![[no title] 66666](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[no title] 66666
![[network counting] 1.4 network delay, packet loss and throughput](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network counting] 1.4 network delay, packet loss and throughput

Automated test series

图的最短路径问题 详细分解版

Detailed decomposition of the shortest path problem in Figure
随机推荐
The mystery of number idempotent and perfect square
LeetCode 8. 字符串转换整数 (atoi)(中等、字符串)
How word removes the header line
圖的最短路徑問題 詳細分解版
The principle and source code interpretation of executor thread pool in concurrent programming
RPC details
数的奥秘之幂数与完全平方数
452. detonate the balloon with the minimum number of arrows
[untitled]
array_column() expects parameter 1 to be array, array given
扎实的基础知识+正确的方法是快速阅读源码的关键
[network planning] 1.3 packet switching and circuit switching in the network core
How about the CSC account of qiniu business school? Is it safe?
[go language learning] - Concurrent Programming
Ts+fetch to upload selected files
How to ensure the sequence of messages, that messages are not lost or consumed repeatedly
String time sorting, sorting time format strings
How to handle file cache and session?
763. dividing alphabetic intervals
LeetCode 1996. 游戏中弱角色的数量*