Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5753599

Browse files
committed
Added README.md file for String to Integer (atoi)
1 parent 5f52386 commit 5753599

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

‎8-string-to-integer-atoi/README.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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>&quot; &quot;</code>).</li>
7+
<li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li>
8+
<li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;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>&nbsp;</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 = &quot;42&quot;</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: &quot;42&quot; (no characters read because there is no leading whitespace)
27+
^
28+
Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)
29+
^
30+
Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; 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 = &quot; -042&quot;</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: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored)
46+
^
47+
Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative)
48+
^
49+
Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; 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 = &quot;1337c0d3&quot;</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: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace)
65+
^
66+
Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)
67+
^
68+
Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; 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 = &quot;0-1&quot;</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: &quot;0-1&quot; (no characters read because there is no leading whitespace)
84+
^
85+
Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)
86+
^
87+
Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; 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 = &quot;words and 987&quot;</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 &#39;w&#39;.</p>
102+
</div>
103+
104+
<p>&nbsp;</p>
105+
<p><strong>Constraints:</strong></p>
106+
107+
<ul>
108+
<li><code>0 &lt;= s.length &lt;= 200</code></li>
109+
<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>
110+
</ul>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /