|
| 1 | +<h2><a href="https://leetcode.com/problems/string-to-integer-atoi">8. String to Integer (atoi)</a></h2><h3>Medium</h3><hr><p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer (similar to C/C++'s <code>atoi</code> function).</p> |
| 2 | + |
| 3 | +<p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> |
| 4 | + |
| 5 | +<ol> |
| 6 | + <li>Read in and ignore any leading whitespace.</li> |
| 7 | + <li>Check if the next character (if not already at the end of the string) is <code>'-'</code> or <code>'+'</code>. 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.</li> |
| 8 | + <li>Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored.</li> |
| 9 | + <li>Convert these digits into an integer (i.e. <code>"123" -> 123</code>, <code>"0032" -> 32</code>). If no digits were read, then the integer is <code>0</code>. Change the sign as necessary (from step 2).</li> |
| 10 | + <li>If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then clamp the integer so that it remains in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be clamped to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be clamped to <code>2<sup>31</sup> - 1</code>.</li> |
| 11 | + <li>Return the integer as the final result.</li> |
| 12 | +</ol> |
| 13 | + |
| 14 | +<p><strong>Note:</strong></p> |
| 15 | + |
| 16 | +<ul> |
| 17 | + <li>Only the space character <code>' '</code> is considered a whitespace character.</li> |
| 18 | + <li><strong>Do not ignore</strong> any characters other than the leading whitespace or the rest of the string after the digits.</li> |
| 19 | +</ul> |
| 20 | + |
| 21 | +<p> </p> |
| 22 | +<p><strong class="example">Example 1:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +<strong>Input:</strong> s = "42" |
| 26 | +<strong>Output:</strong> 42 |
| 27 | +<strong>Explanation:</strong> The underlined characters are what is read in, the caret is the current reader position. |
| 28 | +Step 1: "42" (no characters read because there is no leading whitespace) |
| 29 | + ^ |
| 30 | +Step 2: "42" (no characters read because there is neither a '-' nor '+') |
| 31 | + ^ |
| 32 | +Step 3: "<u>42</u>" ("42" is read in) |
| 33 | + ^ |
| 34 | +The parsed integer is 42. |
| 35 | +Since 42 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 42. |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p><strong class="example">Example 2:</strong></p> |
| 39 | + |
| 40 | +<pre> |
| 41 | +<strong>Input:</strong> s = " -42" |
| 42 | +<strong>Output:</strong> -42 |
| 43 | +<strong>Explanation:</strong> |
| 44 | +Step 1: "<u> </u>-42" (leading whitespace is read and ignored) |
| 45 | + ^ |
| 46 | +Step 2: " <u>-</u>42" ('-' is read, so the result should be negative) |
| 47 | + ^ |
| 48 | +Step 3: " -<u>42</u>" ("42" is read in) |
| 49 | + ^ |
| 50 | +The parsed integer is -42. |
| 51 | +Since -42 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is -42. |
| 52 | +</pre> |
| 53 | + |
| 54 | +<p><strong class="example">Example 3:</strong></p> |
| 55 | + |
| 56 | +<pre> |
| 57 | +<strong>Input:</strong> s = "4193 with words" |
| 58 | +<strong>Output:</strong> 4193 |
| 59 | +<strong>Explanation:</strong> |
| 60 | +Step 1: "4193 with words" (no characters read because there is no leading whitespace) |
| 61 | + ^ |
| 62 | +Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+') |
| 63 | + ^ |
| 64 | +Step 3: "<u>4193</u> with words" ("4193" is read in; reading stops because the next character is a non-digit) |
| 65 | + ^ |
| 66 | +The parsed integer is 4193. |
| 67 | +Since 4193 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 4193. |
| 68 | +</pre> |
| 69 | + |
| 70 | +<p> </p> |
| 71 | +<p><strong>Constraints:</strong></p> |
| 72 | + |
| 73 | +<ul> |
| 74 | + <li><code>0 <= s.length <= 200</code></li> |
| 75 | + <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> |
| 76 | +</ul> |
0 commit comments