|
6 | 6 |
|
7 | 7 | ## Description
|
8 | 8 |
|
9 |
| -<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> |
| 9 | +<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> |
10 | 10 |
|
11 | 11 | <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p>
|
12 | 12 |
|
13 | 13 | <ol>
|
14 |
| - <li>Read in and ignore any leading whitespace.</li> |
15 |
| - <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> |
16 |
| - <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> |
17 |
| - <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> |
18 |
| - <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> |
19 |
| - <li>Return the integer as the final result.</li> |
| 14 | + <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>" "</code>).</li> |
| 15 | + <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> |
| 16 | + <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> |
| 17 | + <li><strong>Edge case</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> |
20 | 18 | </ol>
|
21 | 19 |
|
22 |
| -<p><strong>Note:</strong></p> |
23 |
| - |
24 |
| -<ul> |
25 |
| - <li>Only the space character <code>' '</code> is considered a whitespace character.</li> |
26 |
| - <li><strong>Do not ignore</strong> any characters other than the leading whitespace or the rest of the string after the digits.</li> |
27 |
| -</ul> |
| 20 | +<p>Return the integer as the final result.</p> |
28 | 21 |
|
29 | 22 | <p> </p>
|
30 | 23 | <p><strong class="example">Example 1:</strong></p>
|
31 | 24 |
|
| 25 | +<div class="example-block"> |
| 26 | +<p><strong>Input:</strong> <span class="example-io">s = "42"</span></p> |
| 27 | + |
| 28 | +<p><strong>Output:</strong> <span class="example-io">42</span></p> |
| 29 | + |
| 30 | +<p><strong>Explanation:</strong></p> |
| 31 | + |
32 | 32 | <pre>
|
33 |
| -<strong>Input:</strong> s = "42" |
34 |
| -<strong>Output:</strong> 42 |
35 |
| -<strong>Explanation:</strong> The underlined characters are what is read in, the caret is the current reader position. |
| 33 | +The underlined characters are what is read in and the caret is the current reader position. |
36 | 34 | Step 1: "42" (no characters read because there is no leading whitespace)
|
37 | 35 | ^
|
38 | 36 | Step 2: "42" (no characters read because there is neither a '-' nor '+')
|
39 | 37 | ^
|
40 | 38 | Step 3: "<u>42</u>" ("42" is read in)
|
41 | 39 | ^
|
42 |
| -The parsed integer is 42. |
43 |
| -Since 42 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 42. |
44 | 40 | </pre>
|
| 41 | +</div> |
45 | 42 |
|
46 | 43 | <p><strong class="example">Example 2:</strong></p>
|
47 | 44 |
|
| 45 | +<div class="example-block"> |
| 46 | +<p><strong>Input:</strong> <span class="example-io">s = " -042"</span></p> |
| 47 | + |
| 48 | +<p><strong>Output:</strong> <span class="example-io">-42</span></p> |
| 49 | + |
| 50 | +<p><strong>Explanation:</strong></p> |
| 51 | + |
48 | 52 | <pre>
|
49 |
| -<strong>Input:</strong> s = " -42" |
50 |
| -<strong>Output:</strong> -42 |
51 |
| -<strong>Explanation:</strong> |
52 |
| -Step 1: "<u> </u>-42" (leading whitespace is read and ignored) |
| 53 | +Step 1: "<u> </u>-042" (leading whitespace is read and ignored) |
53 | 54 | ^
|
54 |
| -Step 2: " <u>-</u>42" ('-' is read, so the result should be negative) |
| 55 | +Step 2: " <u>-</u>042" ('-' is read, so the result should be negative) |
55 | 56 | ^
|
56 |
| -Step 3: " -<u>42</u>" ("42" is read in) |
| 57 | +Step 3: " -<u>042</u>" ("042" is read in, leading zeros ignored in the result) |
57 | 58 | ^
|
58 |
| -The parsed integer is -42. |
59 |
| -Since -42 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is -42. |
60 | 59 | </pre>
|
| 60 | +</div> |
61 | 61 |
|
62 | 62 | <p><strong class="example">Example 3:</strong></p>
|
63 | 63 |
|
| 64 | +<div class="example-block"> |
| 65 | +<p><strong>Input:</strong> <span class="example-io">s = "1337c0d3"</span></p> |
| 66 | + |
| 67 | +<p><strong>Output:</strong> <span class="example-io">1337</span></p> |
| 68 | + |
| 69 | +<p><strong>Explanation:</strong></p> |
| 70 | + |
64 | 71 | <pre>
|
65 |
| -<strong>Input:</strong> s = "4193 with words" |
66 |
| -<strong>Output:</strong> 4193 |
67 |
| -<strong>Explanation:</strong> |
68 |
| -Step 1: "4193 with words" (no characters read because there is no leading whitespace) |
| 72 | +Step 1: "1337c0d3" (no characters read because there is no leading whitespace) |
69 | 73 | ^
|
70 |
| -Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+') |
| 74 | +Step 2: "1337c0d3" (no characters read because there is neither a '-' nor '+') |
71 | 75 | ^
|
72 |
| -Step 3: "<u>4193</u> with words" ("4193" is read in; reading stops because the next character is a non-digit) |
| 76 | +Step 3: "<u>1337</u>c0d3" ("1337" is read in; reading stops because the next character is a non-digit) |
73 | 77 | ^
|
74 |
| -The parsed integer is 4193. |
75 |
| -Since 4193 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 4193. |
76 | 78 | </pre>
|
| 79 | +</div> |
| 80 | + |
| 81 | +<p><strong class="example">Example 4:</strong></p> |
| 82 | + |
| 83 | +<div class="example-block"> |
| 84 | +<p><strong>Input:</strong> <span class="example-io">s = "0-1"</span></p> |
| 85 | + |
| 86 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 87 | + |
| 88 | +<p><strong>Explanation:</strong></p> |
| 89 | + |
| 90 | +<pre> |
| 91 | +Step 1: "0-1" (no characters read because there is no leading whitespace) |
| 92 | + ^ |
| 93 | +Step 2: "0-1" (no characters read because there is neither a '-' nor '+') |
| 94 | + ^ |
| 95 | +Step 3: "<u>0</u>-1" ("0" is read in; reading stops because the next character is a non-digit) |
| 96 | + ^ |
| 97 | +</pre> |
| 98 | +</div> |
| 99 | + |
| 100 | +<p><strong class="example">Example 5:</strong></p> |
| 101 | + |
| 102 | +<div class="example-block"> |
| 103 | +<p><strong>Input:</strong> <span class="example-io">s = "words and 987"</span></p> |
| 104 | + |
| 105 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 106 | + |
| 107 | +<p><strong>Explanation:</strong></p> |
| 108 | + |
| 109 | +<p>Reading stops at the first non-digit character 'w'.</p> |
| 110 | +</div> |
77 | 111 |
|
78 | 112 | <p> </p>
|
79 | 113 | <p><strong>Constraints:</strong></p>
|
|
0 commit comments