|
| 1 | +<h2><a href="https://leetcode.com/problems/string-to-integer-atoi">String to Integer (atoi)</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> |
| 2 | + |
| 3 | +<p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> |
| 4 | + |
| 5 | +<ol> |
| 6 | + <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>" "</code>).</li> |
| 7 | + <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>'-'</code> or <code>'+'</code>, assuming positivity is neither present.</li> |
| 8 | + <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> |
| 9 | + <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> |
| 10 | +</ol> |
| 11 | + |
| 12 | +<p>Return the integer as the final result.</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong class="example">Example 1:</strong></p> |
| 16 | + |
| 17 | +<div class="example-block"> |
| 18 | +<p><strong>Input:</strong> <span class="example-io">s = "42"</span></p> |
| 19 | + |
| 20 | +<p><strong>Output:</strong> <span class="example-io">42</span></p> |
| 21 | + |
| 22 | +<p><strong>Explanation:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +The underlined characters are what is read in and 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: "<u>42</u>" ("42" is read in) |
| 31 | + ^ |
| 32 | +</pre> |
| 33 | +</div> |
| 34 | + |
| 35 | +<p><strong class="example">Example 2:</strong></p> |
| 36 | + |
| 37 | +<div class="example-block"> |
| 38 | +<p><strong>Input:</strong> <span class="example-io">s = " -042"</span></p> |
| 39 | + |
| 40 | +<p><strong>Output:</strong> <span class="example-io">-42</span></p> |
| 41 | + |
| 42 | +<p><strong>Explanation:</strong></p> |
| 43 | + |
| 44 | +<pre> |
| 45 | +Step 1: "<u> </u>-042" (leading whitespace is read and ignored) |
| 46 | + ^ |
| 47 | +Step 2: " <u>-</u>042" ('-' is read, so the result should be negative) |
| 48 | + ^ |
| 49 | +Step 3: " -<u>042</u>" ("042" is read in, leading zeros ignored in the result) |
| 50 | + ^ |
| 51 | +</pre> |
| 52 | +</div> |
| 53 | + |
| 54 | +<p><strong class="example">Example 3:</strong></p> |
| 55 | + |
| 56 | +<div class="example-block"> |
| 57 | +<p><strong>Input:</strong> <span class="example-io">s = "1337c0d3"</span></p> |
| 58 | + |
| 59 | +<p><strong>Output:</strong> <span class="example-io">1337</span></p> |
| 60 | + |
| 61 | +<p><strong>Explanation:</strong></p> |
| 62 | + |
| 63 | +<pre> |
| 64 | +Step 1: "1337c0d3" (no characters read because there is no leading whitespace) |
| 65 | + ^ |
| 66 | +Step 2: "1337c0d3" (no characters read because there is neither a '-' nor '+') |
| 67 | + ^ |
| 68 | +Step 3: "<u>1337</u>c0d3" ("1337" is read in; reading stops because the next character is a non-digit) |
| 69 | + ^ |
| 70 | +</pre> |
| 71 | +</div> |
| 72 | + |
| 73 | +<p><strong class="example">Example 4:</strong></p> |
| 74 | + |
| 75 | +<div class="example-block"> |
| 76 | +<p><strong>Input:</strong> <span class="example-io">s = "0-1"</span></p> |
| 77 | + |
| 78 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 79 | + |
| 80 | +<p><strong>Explanation:</strong></p> |
| 81 | + |
| 82 | +<pre> |
| 83 | +Step 1: "0-1" (no characters read because there is no leading whitespace) |
| 84 | + ^ |
| 85 | +Step 2: "0-1" (no characters read because there is neither a '-' nor '+') |
| 86 | + ^ |
| 87 | +Step 3: "<u>0</u>-1" ("0" is read in; reading stops because the next character is a non-digit) |
| 88 | + ^ |
| 89 | +</pre> |
| 90 | +</div> |
| 91 | + |
| 92 | +<p><strong class="example">Example 5:</strong></p> |
| 93 | + |
| 94 | +<div class="example-block"> |
| 95 | +<p><strong>Input:</strong> <span class="example-io">s = "words and 987"</span></p> |
| 96 | + |
| 97 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 98 | + |
| 99 | +<p><strong>Explanation:</strong></p> |
| 100 | + |
| 101 | +<p>Reading stops at the first non-digit character 'w'.</p> |
| 102 | +</div> |
| 103 | + |
| 104 | +<p> </p> |
| 105 | +<p><strong>Constraints:</strong></p> |
| 106 | + |
| 107 | +<ul> |
| 108 | + <li><code>0 <= s.length <= 200</code></li> |
| 109 | + <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> |
| 110 | +</ul> |
0 commit comments