Skip to content

Commit 2f4adee

Browse files
authored
feat: update lc problems (doocs#2710)
1 parent 366b0e0 commit 2f4adee

File tree

57 files changed

+849
-220
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+849
-220
lines changed

solution/0000-0099/0008.String to Integer (atoi)/README_EN.md

+68-34
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,108 @@
66

77
## Description
88

9-
<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>
9+
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p>
1010

1111
<p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p>
1212

1313
<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>&#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>
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>&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>
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>&quot; &quot;</code>).</li>
15+
<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>
16+
<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>
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>
2018
</ol>
2119

22-
<p><strong>Note:</strong></p>
23-
24-
<ul>
25-
<li>Only the space character <code>&#39; &#39;</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>
2821

2922
<p>&nbsp;</p>
3023
<p><strong class="example">Example 1:</strong></p>
3124

25+
<div class="example-block">
26+
<p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p>
27+
28+
<p><strong>Output:</strong> <span class="example-io">42</span></p>
29+
30+
<p><strong>Explanation:</strong></p>
31+
3232
<pre>
33-
<strong>Input:</strong> s = &quot;42&quot;
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.
3634
Step 1: &quot;42&quot; (no characters read because there is no leading whitespace)
3735
^
3836
Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)
3937
^
4038
Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in)
4139
^
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.
4440
</pre>
41+
</div>
4542

4643
<p><strong class="example">Example 2:</strong></p>
4744

45+
<div class="example-block">
46+
<p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p>
47+
48+
<p><strong>Output:</strong> <span class="example-io">-42</span></p>
49+
50+
<p><strong>Explanation:</strong></p>
51+
4852
<pre>
49-
<strong>Input:</strong> s = &quot; -42&quot;
50-
<strong>Output:</strong> -42
51-
<strong>Explanation:</strong>
52-
Step 1: &quot;<u> </u>-42&quot; (leading whitespace is read and ignored)
53+
Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored)
5354
^
54-
Step 2: &quot; <u>-</u>42&quot; (&#39;-&#39; is read, so the result should be negative)
55+
Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative)
5556
^
56-
Step 3: &quot; -<u>42</u>&quot; (&quot;42&quot; is read in)
57+
Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result)
5758
^
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.
6059
</pre>
60+
</div>
6161

6262
<p><strong class="example">Example 3:</strong></p>
6363

64+
<div class="example-block">
65+
<p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p>
66+
67+
<p><strong>Output:</strong> <span class="example-io">1337</span></p>
68+
69+
<p><strong>Explanation:</strong></p>
70+
6471
<pre>
65-
<strong>Input:</strong> s = &quot;4193 with words&quot;
66-
<strong>Output:</strong> 4193
67-
<strong>Explanation:</strong>
68-
Step 1: &quot;4193 with words&quot; (no characters read because there is no leading whitespace)
72+
Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace)
6973
^
70-
Step 2: &quot;4193 with words&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)
74+
Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)
7175
^
72-
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)
76+
Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit)
7377
^
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.
7678
</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 = &quot;0-1&quot;</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: &quot;0-1&quot; (no characters read because there is no leading whitespace)
92+
^
93+
Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)
94+
^
95+
Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; 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 = &quot;words and 987&quot;</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 &#39;w&#39;.</p>
110+
</div>
77111

78112
<p>&nbsp;</p>
79113
<p><strong>Constraints:</strong></p>

solution/0000-0099/0012.Integer to Roman/README_EN.md

+81-28
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,107 @@
66

77
## Description
88

9-
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
10-
11-
<pre>
12-
<strong>Symbol</strong> <strong>Value</strong>
13-
I 1
14-
V 5
15-
X 10
16-
L 50
17-
C 100
18-
D 500
19-
M 1000</pre>
20-
21-
<p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two one&#39;s added together. <code>12</code> is written as&nbsp;<code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
22-
23-
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
9+
<p>Seven different symbols represent Roman numerals with the following values:</p>
10+
11+
<table>
12+
<thead>
13+
<tr>
14+
<th>Symbol</th>
15+
<th>Value</th>
16+
</tr>
17+
</thead>
18+
<tbody>
19+
<tr>
20+
<td>I</td>
21+
<td>1</td>
22+
</tr>
23+
<tr>
24+
<td>V</td>
25+
<td>5</td>
26+
</tr>
27+
<tr>
28+
<td>X</td>
29+
<td>10</td>
30+
</tr>
31+
<tr>
32+
<td>L</td>
33+
<td>50</td>
34+
</tr>
35+
<tr>
36+
<td>C</td>
37+
<td>100</td>
38+
</tr>
39+
<tr>
40+
<td>D</td>
41+
<td>500</td>
42+
</tr>
43+
<tr>
44+
<td>M</td>
45+
<td>1000</td>
46+
</tr>
47+
</tbody>
48+
</table>
49+
50+
<p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p>
2451

2552
<ul>
26-
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li>
27-
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li>
28-
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
53+
<li>If the value does not start with 4 or&nbsp;9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral.</li>
54+
<li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li>
55+
<li>Only powers of 10 (<code>I</code>, <code>X</code>, <code>C</code>, <code>M</code>) can be appended consecutively at most 3 times to represent multiples of 10. You cannot append 5&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li>
2956
</ul>
3057

31-
<p>Given an integer, convert it to a roman numeral.</p>
58+
<p>Given an integer, convert it to a Roman numeral.</p>
3259

3360
<p>&nbsp;</p>
3461
<p><strong class="example">Example 1:</strong></p>
3562

63+
<div class="example-block">
64+
<p><strong>Input:</strong> <span class="example-io">num = 3749</span></p>
65+
66+
<p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p>
67+
68+
<p><strong>Explanation:</strong></p>
69+
3670
<pre>
37-
<strong>Input:</strong> num = 3
38-
<strong>Output:</strong> &quot;III&quot;
39-
<strong>Explanation:</strong> 3 is represented as 3 ones.
71+
3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M)
72+
700 = DCC as 500 (D) + 100 (C) + 100 (C)
73+
40 = XL as 10 (X) less of 50 (L)
74+
9 = IX as 1 (I) less of 10 (X)
75+
Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places
4076
</pre>
77+
</div>
4178

4279
<p><strong class="example">Example 2:</strong></p>
4380

81+
<div class="example-block">
82+
<p><strong>Input:</strong> <span class="example-io">num = 58</span></p>
83+
84+
<p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p>
85+
86+
<p><strong>Explanation:</strong></p>
87+
4488
<pre>
45-
<strong>Input:</strong> num = 58
46-
<strong>Output:</strong> &quot;LVIII&quot;
47-
<strong>Explanation:</strong> L = 50, V = 5, III = 3.
89+
50 = L
90+
8 = VIII
4891
</pre>
92+
</div>
4993

5094
<p><strong class="example">Example 3:</strong></p>
5195

96+
<div class="example-block">
97+
<p><strong>Input:</strong> <span class="example-io">num = 1994</span></p>
98+
99+
<p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p>
100+
101+
<p><strong>Explanation:</strong></p>
102+
52103
<pre>
53-
<strong>Input:</strong> num = 1994
54-
<strong>Output:</strong> &quot;MCMXCIV&quot;
55-
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
104+
1000 = M
105+
900 = CM
106+
90 = XC
107+
4 = IV
56108
</pre>
109+
</div>
57110

58111
<p>&nbsp;</p>
59112
<p><strong>Constraints:</strong></p>

solution/0000-0099/0038.Count and Say/README_EN.md

+27-17
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,42 @@
1010

1111
<ul>
1212
<li><code>countAndSay(1) = &quot;1&quot;</code></li>
13-
<li><code>countAndSay(n)</code> is the way you would &quot;say&quot; the digit string from <code>countAndSay(n-1)</code>, which is then converted into a different digit string.</li>
13+
<li><code>countAndSay(n)</code> is the run-length encoding of <code>countAndSay(n - 1)</code>.</li>
1414
</ul>
1515

16-
<p>To determine how you &quot;say&quot; a digit string, split it into the <strong>minimal</strong> number of substrings such that each substring contains exactly <strong>one</strong> unique digit. Then for each substring, say the number of digits, then say the digit. Finally, concatenate every said digit.</p>
16+
<p><a href="http://en.wikipedia.org/wiki/Run-length_encoding" target="_blank">Run-length encoding</a> (RLE) is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string <code>&quot;3322251&quot;</code> we replace <code>&quot;33&quot;</code> with <code>&quot;23&quot;</code>, replace <code>&quot;222&quot;</code> with <code>&quot;32&quot;</code>, replace <code>&quot;5&quot;</code> with <code>&quot;15&quot;</code> and replace <code>&quot;1&quot;</code> with <code>&quot;11&quot;</code>. Thus the compressed string becomes <code>&quot;23321511&quot;</code>.</p>
1717

18-
<p>For example, the saying and conversion for digit string <code>&quot;3322251&quot;</code>:</p>
19-
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0038.Count%20and%20Say/images/countandsay.jpg" style="width: 581px; height: 172px;" />
20-
<p>Given a positive integer <code>n</code>, return <em>the </em><code>n<sup>th</sup></code><em> term of the <strong>count-and-say</strong> sequence</em>.</p>
18+
<p>Given a positive integer <code>n</code>, return <em>the </em><code>n<sup>th</sup></code><em> element of the <strong>count-and-say</strong> sequence</em>.</p>
2119

2220
<p>&nbsp;</p>
2321
<p><strong class="example">Example 1:</strong></p>
2422

25-
<pre>
26-
<strong>Input:</strong> n = 1
27-
<strong>Output:</strong> &quot;1&quot;
28-
<strong>Explanation:</strong> This is the base case.
29-
</pre>
23+
<div class="example-block">
24+
<p><strong>Input:</strong> <span class="example-io">n = 4</span></p>
3025

31-
<p><strong class="example">Example 2:</strong></p>
26+
<p><strong>Output:</strong> <span class="example-io">&quot;1211&quot;</span></p>
27+
28+
<p><strong>Explanation:</strong></p>
3229

3330
<pre>
34-
<strong>Input:</strong> n = 4
35-
<strong>Output:</strong> &quot;1211&quot;
36-
<strong>Explanation:</strong>
3731
countAndSay(1) = &quot;1&quot;
38-
countAndSay(2) = say &quot;1&quot; = one 1 = &quot;11&quot;
39-
countAndSay(3) = say &quot;11&quot; = two 1&#39;s = &quot;21&quot;
40-
countAndSay(4) = say &quot;21&quot; = one 2 + one 1 = &quot;12&quot; + &quot;11&quot; = &quot;1211&quot;
32+
countAndSay(2) = RLE of &quot;1&quot; = &quot;11&quot;
33+
countAndSay(3) = RLE of &quot;11&quot; = &quot;21&quot;
34+
countAndSay(4) = RLE of &quot;21&quot; = &quot;1211&quot;
4135
</pre>
36+
</div>
37+
38+
<p><strong class="example">Example 2:</strong></p>
39+
40+
<div class="example-block">
41+
<p><strong>Input:</strong> <span class="example-io">n = 1</span></p>
42+
43+
<p><strong>Output:</strong> <span class="example-io">&quot;1&quot;</span></p>
44+
45+
<p><strong>Explanation:</strong></p>
46+
47+
<p>This is the base case.</p>
48+
</div>
4249

4350
<p>&nbsp;</p>
4451
<p><strong>Constraints:</strong></p>
@@ -47,6 +54,9 @@ countAndSay(4) = say &quot;21&quot; = one 2 + one 1 = &quot;12&quot; + &quot;11&
4754
<li><code>1 &lt;= n &lt;= 30</code></li>
4855
</ul>
4956

57+
<p>&nbsp;</p>
58+
<strong>Follow up:</strong> Could you solve it iteratively?
59+
5060
## Solutions
5161

5262
### Solution 1: Simulation

0 commit comments

Comments
 (0)