Skip to content

Commit e7675c1

Browse files
committed
Create README - LeetHub
1 parent 35bc865 commit e7675c1

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

0008-string-to-integer-atoi/README.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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++&#39;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>&#39;-&#39;</code> or <code>&#39;+&#39;</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>&quot;123&quot; -&gt; 123</code>, <code>&quot;0032&quot; -&gt; 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>&#39; &#39;</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>&nbsp;</p>
22+
<p><strong class="example">Example 1:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> s = &quot;42&quot;
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: &quot;42&quot; (no characters read because there is no leading whitespace)
29+
^
30+
Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)
31+
^
32+
Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; 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 = &quot; -42&quot;
42+
<strong>Output:</strong> -42
43+
<strong>Explanation:</strong>
44+
Step 1: &quot;<u> </u>-42&quot; (leading whitespace is read and ignored)
45+
^
46+
Step 2: &quot; <u>-</u>42&quot; (&#39;-&#39; is read, so the result should be negative)
47+
^
48+
Step 3: &quot; -<u>42</u>&quot; (&quot;42&quot; 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 = &quot;4193 with words&quot;
58+
<strong>Output:</strong> 4193
59+
<strong>Explanation:</strong>
60+
Step 1: &quot;4193 with words&quot; (no characters read because there is no leading whitespace)
61+
^
62+
Step 2: &quot;4193 with words&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)
63+
^
64+
Step 3: &quot;<u>4193</u> with words&quot; (&quot;4193&quot; 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>&nbsp;</p>
71+
<p><strong>Constraints:</strong></p>
72+
73+
<ul>
74+
<li><code>0 &lt;= s.length &lt;= 200</code></li>
75+
<li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li>
76+
</ul>

0 commit comments

Comments
 (0)