2025-01-09 20:29:41 +08:00
< p > Implement the < code > myAtoi(string s)< / code > function, which converts a string to a 32-bit signed integer.< / p >
2022-03-27 20:56:26 +08:00
< p > The algorithm for < code > myAtoi(string s)< / code > is as follows:< / p >
< ol >
2025-01-09 20:29:41 +08:00
< li > < strong > Whitespace< / strong > : Ignore any leading whitespace (< code > " " < / code > ).< / li >
< li > < strong > Signedness< / strong > : Determine the sign by checking if the next character is < code > ' -' < / code > or < code > ' +' < / code > , assuming positivity if neither present.< / li >
< li > < strong > Conversion< / strong > : Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.< / li >
< li > < strong > Rounding< / strong > : If the integer is out of the 32-bit signed integer range < code > [-2< sup > 31< / sup > , 2< sup > 31< / sup > - 1]< / code > , then round the integer to remain in the range. Specifically, integers less than < code > -2< sup > 31< / sup > < / code > should be rounded to < code > -2< sup > 31< / sup > < / code > , and integers greater than < code > 2< sup > 31< / sup > - 1< / code > should be rounded to < code > 2< sup > 31< / sup > - 1< / code > .< / li >
2022-03-27 20:56:26 +08:00
< / ol >
2025-01-09 20:29:41 +08:00
< p > Return the integer as the final result.< / p >
2022-03-27 20:56:26 +08:00
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 20:56:26 +08:00
2025-01-09 20:29:41 +08:00
< div class = "example-block" >
< p > < strong > Input:< / strong > < span class = "example-io" > s = " 42" < / span > < / p >
< p > < strong > Output:< / strong > < span class = "example-io" > 42< / span > < / p >
< p > < strong > Explanation:< / strong > < / p >
2022-03-27 20:56:26 +08:00
< pre >
2025-01-09 20:29:41 +08:00
The underlined characters are what is read in and the caret is the current reader position.
2022-03-27 20:56:26 +08:00
Step 1: " 42" (no characters read because there is no leading whitespace)
^
Step 2: " 42" (no characters read because there is neither a ' -' nor ' +' )
^
Step 3: " < u > 42< / u > " (" 42" is read in)
^
< / pre >
2025-01-09 20:29:41 +08:00
< / div >
2022-03-27 20:56:26 +08:00
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 20:56:26 +08:00
2025-01-09 20:29:41 +08:00
< div class = "example-block" >
< p > < strong > Input:< / strong > < span class = "example-io" > s = " -042" < / span > < / p >
< p > < strong > Output:< / strong > < span class = "example-io" > -42< / span > < / p >
< p > < strong > Explanation:< / strong > < / p >
2022-03-27 20:56:26 +08:00
< pre >
2025-01-09 20:29:41 +08:00
Step 1: " < u > < / u > -042" (leading whitespace is read and ignored)
2022-03-27 20:56:26 +08:00
^
2025-01-09 20:29:41 +08:00
Step 2: " < u > -< / u > 042" (' -' is read, so the result should be negative)
2022-03-27 20:56:26 +08:00
^
2025-01-09 20:29:41 +08:00
Step 3: " -< u > 042< / u > " (" 042" is read in, leading zeros ignored in the result)
2022-03-27 20:56:26 +08:00
^
< / pre >
2025-01-09 20:29:41 +08:00
< / div >
2022-03-27 20:56:26 +08:00
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 3:< / strong > < / p >
2022-03-27 20:56:26 +08:00
2025-01-09 20:29:41 +08:00
< div class = "example-block" >
< p > < strong > Input:< / strong > < span class = "example-io" > s = " 1337c0d3" < / span > < / p >
< p > < strong > Output:< / strong > < span class = "example-io" > 1337< / span > < / p >
< p > < strong > Explanation:< / strong > < / p >
2022-03-27 20:56:26 +08:00
< pre >
2025-01-09 20:29:41 +08:00
Step 1: " 1337c0d3" (no characters read because there is no leading whitespace)
2022-03-27 20:56:26 +08:00
^
2025-01-09 20:29:41 +08:00
Step 2: " 1337c0d3" (no characters read because there is neither a ' -' nor ' +' )
2022-03-27 20:56:26 +08:00
^
2025-01-09 20:29:41 +08:00
Step 3: " < u > 1337< / u > c0d3" (" 1337" is read in; reading stops because the next character is a non-digit)
2022-03-27 20:56:26 +08:00
^
< / pre >
2025-01-09 20:29:41 +08:00
< / div >
< p > < strong class = "example" > Example 4:< / strong > < / p >
< div class = "example-block" >
< p > < strong > Input:< / strong > < span class = "example-io" > s = " 0-1" < / span > < / p >
< p > < strong > Output:< / strong > < span class = "example-io" > 0< / span > < / p >
< p > < strong > Explanation:< / strong > < / p >
< pre >
Step 1: " 0-1" (no characters read because there is no leading whitespace)
^
Step 2: " 0-1" (no characters read because there is neither a ' -' nor ' +' )
^
Step 3: " < u > 0< / u > -1" (" 0" is read in; reading stops because the next character is a non-digit)
^
< / pre >
< / div >
< p > < strong class = "example" > Example 5:< / strong > < / p >
< div class = "example-block" >
< p > < strong > Input:< / strong > < span class = "example-io" > s = " words and 987" < / span > < / p >
< p > < strong > Output:< / strong > < span class = "example-io" > 0< / span > < / p >
< p > < strong > Explanation:< / strong > < / p >
< p > Reading stops at the first non-digit character ' w' .< / p >
< / div >
2022-03-27 20:56:26 +08:00
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > 0 < = s.length < = 200< / code > < / li >
< li > < code > s< / code > consists of English letters (lower-case and upper-case), digits (< code > 0-9< / code > ), < code > ' ' < / code > , < code > ' +' < / code > , < code > ' -' < / code > , and < code > ' .' < / code > .< / li >
< / ul >