|
| 1 | +# String to Int <https://leetcode.com/problems/string-to-integer-atoi/> |
| 2 | + |
| 3 | +Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function). |
| 4 | + |
| 5 | +The algorithm for myAtoi(string s) is as follows: |
| 6 | + |
| 7 | +1. Read in and ignore any leading whitespace. |
| 8 | +1. Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present. |
| 9 | +1. Read in next the characters until the next non-digit charcter or the end of the input is reached. The rest of the string is ignored. |
| 10 | +1. Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2). |
| 11 | +1. If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1. |
| 12 | +1. Return the integer as the final result. |
| 13 | + |
| 14 | +## Note: |
| 15 | + |
| 16 | +Only the space character ' ' is considered a whitespace character. |
| 17 | +Do not ignore any characters other than the leading whitespace or the rest of the string after the digits. |
| 18 | + |
| 19 | + |
| 20 | +## Example 1: |
| 21 | + |
| 22 | +``` |
| 23 | +Input: s = "42" |
| 24 | +Output: 42 |
| 25 | +Explanation: The underlined characters are what is read in, the caret is the current reader position. |
| 26 | +Step 1: "42" (no characters read because there is no leading whitespace) |
| 27 | + ^ |
| 28 | +Step 2: "42" (no characters read because there is neither a '-' nor '+') |
| 29 | + ^ |
| 30 | +Step 3: "42" ("42" is read in) |
| 31 | + ^ |
| 32 | +The parsed integer is 42. |
| 33 | +Since 42 is in the range [-231, 231 - 1], the final result is 42. |
| 34 | +``` |
| 35 | + |
| 36 | +## Example 2: |
| 37 | + |
| 38 | +``` |
| 39 | +Input: s = " -42" |
| 40 | +Output: -42 |
| 41 | +Explanation: |
| 42 | +Step 1: " -42" (leading whitespace is read and ignored) |
| 43 | + ^ |
| 44 | +Step 2: " -42" ('-' is read, so the result should be negative) |
| 45 | + ^ |
| 46 | +Step 3: " -42" ("42" is read in) |
| 47 | + ^ |
| 48 | +The parsed integer is -42. |
| 49 | +Since -42 is in the range [-231, 231 - 1], the final result is -42. |
| 50 | +``` |
| 51 | + |
| 52 | +## Example 3: |
| 53 | + |
| 54 | +``` |
| 55 | +Input: s = "4193 with words" |
| 56 | +Output: 4193 |
| 57 | +Explanation: |
| 58 | +Step 1: "4193 with words" (no characters read because there is no leading whitespace) |
| 59 | + ^ |
| 60 | +Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+') |
| 61 | + ^ |
| 62 | +Step 3: "4193 with words" ("4193" is read in; reading stops because the next character is a non-digit) |
| 63 | + ^ |
| 64 | +The parsed integer is 4193. |
| 65 | +Since 4193 is in the range [-231, 231 - 1], the final result is 4193. |
| 66 | +``` |
| 67 | + |
| 68 | +## Example 4: |
| 69 | + |
| 70 | +``` |
| 71 | +Input: s = "words and 987" |
| 72 | +Output: 0 |
| 73 | +Explanation: |
| 74 | +Step 1: "words and 987" (no characters read because there is no leading whitespace) |
| 75 | + ^ |
| 76 | +Step 2: "words and 987" (no characters read because there is neither a '-' nor '+') |
| 77 | + ^ |
| 78 | +Step 3: "words and 987" (reading stops immediately because there is a non-digit 'w') |
| 79 | + ^ |
| 80 | +The parsed integer is 0 because no digits were read. |
| 81 | +Since 0 is in the range [-231, 231 - 1], the final result is 0. |
| 82 | +``` |
| 83 | + |
| 84 | +## Example 5: |
| 85 | + |
| 86 | +``` |
| 87 | +Input: s = "-91283472332" |
| 88 | +Output: -2147483648 |
| 89 | +Explanation: |
| 90 | +Step 1: "-91283472332" (no characters read because there is no leading whitespace) |
| 91 | + ^ |
| 92 | +Step 2: "-91283472332" ('-' is read, so the result should be negative) |
| 93 | + ^ |
| 94 | +Step 3: "-91283472332" ("91283472332" is read in) |
| 95 | + ^ |
| 96 | +The parsed integer is -91283472332. |
| 97 | +Since -91283472332 is less than the lower bound of the range [-231, 231 - 1], the final result is clamped to -231 = -2147483648. |
| 98 | +``` |
| 99 | + |
| 100 | +## Constraints: |
| 101 | + |
| 102 | +0 <= s.length <= 200 |
| 103 | +s consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'. |
0 commit comments