Skip to content

Commit ce92293

Browse files
authored
feat: update lc problems and solutions (doocs#1136)
1 parent 7aeedec commit ce92293

File tree

69 files changed

+1282
-658
lines changed

Some content is hidden

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

69 files changed

+1282
-658
lines changed

solution/0100-0199/0176.Second Highest Salary/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ Each row of this table contains information about the salary of an employee.
1919

2020
<p>&nbsp;</p>
2121

22-
<p>Write an SQL query to report the second highest salary from the <code>Employee</code> table. If there is no second highest salary, the query should report <code>null</code>.</p>
22+
<p>Find&nbsp;the second highest salary from the <code>Employee</code> table. If there is no second highest salary,&nbsp;return&nbsp;<code>null</code>.</p>
2323

24-
<p>The query result format is in the following example.</p>
24+
<p>The result format is in the following example.</p>
2525

2626
<p>&nbsp;</p>
2727
<p><strong class="example">Example 1:</strong></p>

solution/0100-0199/0177.Nth Highest Salary/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ Each row of this table contains information about the salary of an employee.
1919

2020
<p>&nbsp;</p>
2121

22-
<p>Write an SQL query to report the <code>n<sup>th</sup></code> highest salary from the <code>Employee</code> table. If there is no <code>n<sup>th</sup></code> highest salary, the query should report <code>null</code>.</p>
22+
<p>Find the <code>n<sup>th</sup></code> highest salary from the <code>Employee</code> table. If there is no <code>n<sup>th</sup></code> highest salary, return&nbsp;<code>null</code>.</p>
2323

24-
<p>The query result format is in the following example.</p>
24+
<p>The result format is in the following example.</p>
2525

2626
<p>&nbsp;</p>
2727
<p><strong class="example">Example 1:</strong></p>

solution/0100-0199/0178.Rank Scores/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Each row of this table contains the score of a game. Score is a floating point v
1919

2020
<p>&nbsp;</p>
2121

22-
<p>Write an SQL query to rank the scores. The ranking should be calculated according to the following rules:</p>
22+
<p>Find the rank of the scores. The ranking should be calculated according to the following rules:</p>
2323

2424
<ul>
2525
<li>The scores should be ranked from the highest to the lowest.</li>
@@ -29,7 +29,7 @@ Each row of this table contains the score of a game. Score is a floating point v
2929

3030
<p>Return the result table ordered by <code>score</code> in descending order.</p>
3131

32-
<p>The query result format is in the following example.</p>
32+
<p>The result format is in the following example.</p>
3333

3434
<p>&nbsp;</p>
3535
<p><strong class="example">Example 1:</strong></p>

solution/0100-0199/0183.Customers Who Never Order/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ Each row of this table indicates the ID of an order and the ID of the customer w
3535

3636
<p>&nbsp;</p>
3737

38-
<p>Write an SQL query to report all customers who never order anything.</p>
38+
<p>Find all customers who never order anything.</p>
3939

4040
<p>Return the result table in <strong>any order</strong>.</p>
4141

42-
<p>The query result format is in the following example.</p>
42+
<p>The result format is in the following example.</p>
4343

4444
<p>&nbsp;</p>
4545
<p><strong class="example">Example 1:</strong></p>

solution/0100-0199/0184.Department Highest Salary/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ Each row of this table indicates the ID of a department and its name.
3737

3838
<p>&nbsp;</p>
3939

40-
<p>Write an SQL query to find employees who have the highest salary in each of the departments.</p>
40+
<p>Find employees who have the highest salary in each of the departments.</p>
4141

4242
<p>Return the result table in <strong>any order</strong>.</p>
4343

44-
<p>The query result format is in the following example.</p>
44+
<p>The result format is in the following example.</p>
4545

4646
<p>&nbsp;</p>
4747
<p><strong class="example">Example 1:</strong></p>

solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ Each row of this table contains an email. The emails will not contain uppercase
1919

2020
<p>&nbsp;</p>
2121

22-
<p>Write an SQL query to <strong>delete</strong> all the duplicate emails, keeping only one unique email with the smallest <code>id</code>. Note that you are supposed to write a <code>DELETE</code> statement and not a <code>SELECT</code> one.</p>
22+
<p><strong>Delete</strong> all the duplicate emails, keeping only one unique email with the smallest <code>id</code>. (For SQL users, please note that you are supposed to write a <code>DELETE</code> statement and not a <code>SELECT</code> one.)</p>
2323

2424
<p>After running your script, the answer shown is the <code>Person</code> table. The driver will first compile and run your piece of code and then show the <code>Person</code> table. The final order of the <code>Person</code> table <strong>does not matter</strong>.</p>
2525

26-
<p>The query result format is in the following example.</p>
26+
<p>The result format is in the following example.</p>
2727

2828
<p>&nbsp;</p>
2929
<p><strong class="example">Example 1:</strong></p>

solution/0300-0399/0304.Range Sum Query 2D - Immutable/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public:
175175
*/
176176
177177
struct NumMatrix {
178-
// Of size (N + 1) * (M + 1)
178+
// Of size (N + 1) * (M + 1)
179179
prefix_vec: Vec<Vec<i32>>,
180180
n: usize,
181181
m: usize,
@@ -184,22 +184,22 @@ struct NumMatrix {
184184
}
185185
186186
187-
/**
187+
/**
188188
* `&self` means the method takes an immutable reference.
189189
* If you need a mutable reference, change it to `&mut self` instead.
190190
*/
191191
impl NumMatrix {
192192
193193
fn new(matrix: Vec<Vec<i32>>) -> Self {
194-
NumMatrix {
194+
NumMatrix {
195195
prefix_vec: vec![vec![0; matrix[0].len() + 1]; matrix.len() + 1],
196196
n: matrix.len(),
197197
m: matrix[0].len(),
198198
is_initialized: false,
199199
ref_vec: matrix,
200200
}
201201
}
202-
202+
203203
fn sum_region(&mut self, row1: i32, col1: i32, row2: i32, col2: i32) -> i32 {
204204
if !self.is_initialized {
205205
self.initialize_prefix_vec();

solution/0300-0399/0304.Range Sum Query 2D - Immutable/README_EN.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle)
5252

5353
## Solutions
5454

55-
We use $s[i + 1][j + 1]$ to represent the sum of all elements in the upper-left part up to the $i$-th row and $j$-th column, where the indices $i$ and $j$ both start from $0$.
55+
We use $s[i + 1][j + 1]$ to represent the sum of all elements in the upper-left part up to the $i$-th row and $j$-th column, where the indices $i$ and $j$ both start from $0$.
5656

5757
We can derive the following prefix sum formula:
5858

@@ -166,7 +166,7 @@ public:
166166
*/
167167
168168
struct NumMatrix {
169-
// Of size (N + 1) * (M + 1)
169+
// Of size (N + 1) * (M + 1)
170170
prefix_vec: Vec<Vec<i32>>,
171171
n: usize,
172172
m: usize,
@@ -175,22 +175,22 @@ public:
175175
}
176176
177177
178-
/**
178+
/**
179179
* `&self` means the method takes an immutable reference.
180180
* If you need a mutable reference, change it to `&mut self` instead.
181181
*/
182182
impl NumMatrix {
183183
184184
fn new(matrix: Vec<Vec<i32>>) -> Self {
185-
NumMatrix {
185+
NumMatrix {
186186
prefix_vec: vec![vec![0; matrix[0].len() + 1]; matrix.len() + 1],
187187
n: matrix.len(),
188188
m: matrix[0].len(),
189189
is_initialized: false,
190190
ref_vec: matrix,
191191
}
192192
}
193-
193+
194194
fn sum_region(&mut self, row1: i32, col1: i32, row2: i32, col2: i32) -> i32 {
195195
if !self.is_initialized {
196196
self.initialize_prefix_vec();

solution/0300-0399/0373.Find K Pairs with Smallest Sums/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给定两个以 <strong>升序排列</strong> 的整数数组 <code>nums1</code> 和<strong> </strong><code>nums2</code><strong>&nbsp;</strong>,&nbsp;以及一个整数 <code>k</code><strong>&nbsp;</strong>。</p>
9+
<p>给定两个以 <strong>非递减顺序排列</strong> 的整数数组 <code>nums1</code> 和<strong> </strong><code>nums2</code><strong>&nbsp;</strong>,&nbsp;以及一个整数 <code>k</code><strong>&nbsp;</strong>。</p>
1010

1111
<p>定义一对值&nbsp;<code>(u,v)</code>,其中第一个元素来自&nbsp;<code>nums1</code>,第二个元素来自 <code>nums2</code><strong>&nbsp;</strong>。</p>
1212

1313
<p>请找到和最小的 <code>k</code>&nbsp;个数对&nbsp;<code>(u<sub>1</sub>,v<sub>1</sub>)</code>, <code>&nbsp;(u<sub>2</sub>,v<sub>2</sub>)</code> &nbsp;... &nbsp;<code>(u<sub>k</sub>,v<sub>k</sub>)</code>&nbsp;。</p>
1414

1515
<p>&nbsp;</p>
1616

17-
<p><strong>示例 1:</strong></p>
17+
<p><strong class="example">示例 1:</strong></p>
1818

1919
<pre>
2020
<strong>输入:</strong> nums1 = [1,7,11], nums2 = [2,4,6], k = 3
@@ -23,7 +23,7 @@
2323
[1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
2424
</pre>
2525

26-
<p><strong>示例 2:</strong></p>
26+
<p><strong class="example">示例 2:</strong></p>
2727

2828
<pre>
2929
<strong>输入: </strong>nums1 = [1,1,2], nums2 = [1,2,3], k = 2
@@ -32,7 +32,7 @@
3232
&nbsp; [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
3333
</pre>
3434

35-
<p><strong>示例 3:</strong></p>
35+
<p><strong class="example">示例 3:</strong></p>
3636

3737
<pre>
3838
<strong>输入: </strong>nums1 = [1,2], nums2 = [3], k = 3

solution/0500-0599/0511.Game Play Analysis I/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ Each row is a record of a player who logged in and played a number of games (pos
2222

2323
<p>&nbsp;</p>
2424

25-
<p>Write an SQL query to report the <strong>first login date</strong> for each player.</p>
25+
<p>Find the <strong>first login date</strong> for each player.</p>
2626

2727
<p>Return the result table in <strong>any order</strong>.</p>
2828

29-
<p>The query result format is in the following example.</p>
29+
<p>The result format is in the following example.</p>
3030

3131
<p>&nbsp;</p>
3232
<p><strong class="example">Example 1:</strong></p>

solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ No employee will be the manager of themself.
2323

2424
<p>&nbsp;</p>
2525

26-
<p>Write an SQL query to report the managers with at least <strong>five direct reports</strong>.</p>
26+
<p>Find the managers with at least <strong>five direct reports</strong>.</p>
2727

2828
<p>Return the result table in <strong>any order</strong>.</p>
2929

30-
<p>The query result format is in the following example.</p>
30+
<p>The result format is in the following example.</p>
3131

3232
<p>&nbsp;</p>
3333
<p><strong class="example">Example 1:</strong></p>

solution/0500-0599/0585.Investments in 2016/README.md

+57-45
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,68 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>写一个查询语句,将&nbsp;2016 年 (<strong>TIV_2016</strong>) 所有成功投资的金额加起来,保留 2 位小数。</p>
10-
11-
<p>对于一个投保人,他在 2016 年成功投资的条件是:</p>
12-
13-
<ol>
14-
<li>他在 2015 年的投保额&nbsp;(<strong>TIV_2015</strong>) 至少跟一个其他投保人在 2015 年的投保额相同。</li>
15-
<li>他所在的城市必须与其他投保人都不同(也就是说维度和经度不能跟其他任何一个投保人完全相同)。</li>
16-
</ol>
17-
18-
<p><strong>输入格式:</strong><br>
19-
&nbsp;<strong><em>insurance</em></strong> 格式如下:</p>
20-
21-
<pre>| Column Name | Type |
22-
|-------------|---------------|
23-
| PID | INTEGER(11) |
24-
| TIV_2015 | NUMERIC(15,2) |
25-
| TIV_2016 | NUMERIC(15,2) |
26-
| LAT | NUMERIC(5,2) |
27-
| LON | NUMERIC(5,2) |
28-
</pre>
29-
30-
<p><strong>PID</strong>&nbsp;字段是投保人的投保编号,&nbsp;<strong>TIV_2015</strong> 是该投保人在2015年的总投保金额,&nbsp;<strong>TIV_2016</strong> 是该投保人在2016年的投保金额,&nbsp;<strong>LAT</strong> 是投保人所在城市的维度,&nbsp;<strong>LON</strong>&nbsp;是投保人所在城市的经度。</p>
31-
32-
<p><strong>样例输入</strong></p>
33-
34-
<pre>| PID | TIV_2015 | TIV_2016 | LAT | LON |
35-
|-----|----------|----------|-----|-----|
9+
<p><code>Insurance</code> 表:</p>
10+
11+
<div class="original__bRMd">
12+
<div>
13+
<pre>
14+
+-------------+-------+
15+
| Column Name | Type |
16+
+-------------+-------+
17+
| pid | int |
18+
| tiv_2015 | float |
19+
| tiv_2016 | float |
20+
| lat | float |
21+
| lon | float |
22+
+-------------+-------+
23+
pid 是这张表的主键。
24+
表中的每一行都包含一条保险信息,其中:
25+
pid 是投保人的投保编号。
26+
tiv_2015 是该投保人在 2015 年的总投保金额,tiv_2016 是该投保人在 2016 年的总投保金额。
27+
lat 是投保人所在城市的纬度。题目数据确保 lat 不为空。
28+
lon 是投保人所在城市的经度。题目数据确保 lon 不为空。</pre>
29+
30+
<p>&nbsp;</p>
31+
32+
<p>请你编写一个 SQL 查询,报告 2016 年 (<code>tiv_2016</code>) 所有满足下述条件的投保人的投保金额之和:</p>
33+
34+
<ul>
35+
<li>他在 2015 年的投保额&nbsp;(<code>tiv_2015</code>) 至少跟一个其他投保人在 2015 年的投保额相同。</li>
36+
<li>他所在的城市必须与其他投保人都不同(也就是说&nbsp;(<code>lat, lon</code>) 不能跟其他任何一个投保人完全相同)。</li>
37+
</ul>
38+
39+
<p><code>tiv_2016</code> 四舍五入的 <strong>两位小数</strong> 。</p>
40+
41+
<p>查询结果格式如下例所示。</p>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong class="example">示例:</strong></p>
46+
47+
<pre>
48+
<strong>输入:</strong>
49+
Insurance 表:
50+
+-----+----------+----------+-----+-----+
51+
| pid | tiv_2015 | tiv_2016 | lat | lon |
52+
+-----+----------+----------+-----+-----+
3653
| 1 | 10 | 5 | 10 | 10 |
3754
| 2 | 20 | 20 | 20 | 20 |
3855
| 3 | 10 | 30 | 20 | 20 |
3956
| 4 | 10 | 40 | 40 | 40 |
40-
</pre>
41-
42-
<p><strong>样例输出</strong></p>
43-
44-
<pre>| TIV_2016 |
45-
|----------|
57+
+-----+----------+----------+-----+-----+
58+
<strong>输出:</strong>
59+
+----------+
60+
| tiv_2016 |
61+
+----------+
4662
| 45.00 |
47-
</pre>
48-
49-
<p><strong>解释</strong></p>
50-
51-
<pre>就如最后一个投保人,第一个投保人同时满足两个条件:
52-
1. 他在 2015 年的投保金额 <strong>TIV_2015 </strong>为 &#39;10&#39; ,与第三个和第四个投保人在 2015 年的投保金额相同。
53-
2. 他所在城市的经纬度是独一无二的。
54-
55-
第二个投保人两个条件都不满足。他在 2015 年的投资 <strong>TIV_2015 </strong>与其他任何投保人都不相同。
56-
且他所在城市的经纬度与第三个投保人相同。基于同样的原因,第三个投保人投资失败。
57-
58-
所以返回的结果是第一个投保人和最后一个投保人的 <strong>TIV_2016 </strong>之和,结果是 45 。</pre>
63+
+----------+
64+
<strong>解释:
65+
</strong>表中的第一条记录和最后一条记录都满足两个条件。
66+
tiv_2015 值为 10 与第三条和第四条记录相同,且其位置是唯一的。
67+
第二条记录不符合任何一个条件。其 tiv_2015 与其他投保人不同,并且位置与第三条记录相同,这也导致了第三条记录不符合题目要求。
68+
因此,结果是第一条记录和最后一条记录的 tiv_2016 之和,即 45 。</pre>
69+
</div>
70+
</div>
5971

6072
## 解法
6173

solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ This table contains information about the order ID and the customer ID.
1919

2020
<p>&nbsp;</p>
2121

22-
<p>Write an SQL query to find the <code>customer_number</code> for the customer who has placed <strong>the largest number of orders</strong>.</p>
22+
<p>Find the <code>customer_number</code> for the customer who has placed <strong>the largest number of orders</strong>.</p>
2323

2424
<p>The test cases are generated so that <strong>exactly one customer</strong> will have placed more orders than any other customer.</p>
2525

26-
<p>The query result format is in the following example.</p>
26+
<p>The result format is in the following example.</p>
2727

2828
<p>&nbsp;</p>
2929
<p><strong class="example">Example 1:</strong></p>

solution/0500-0599/0595.Big Countries/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ Each row of this table gives information about the name of a country, the contin
2929
<li>it has a population of at least&nbsp;twenty-five million (i.e., <code>25000000</code>).</li>
3030
</ul>
3131

32-
<p>Write an SQL query to report the name, population, and area of the <strong>big countries</strong>.</p>
32+
<p>Find the name, population, and area of the <strong>big countries</strong>.</p>
3333

3434
<p>Return the result table in <strong>any order</strong>.</p>
3535

36-
<p>The query result format is in the following example.</p>
36+
<p>The result format is in the following example.</p>
3737

3838
<p>&nbsp;</p>
3939
<p><strong class="example">Example 1:</strong></p>

solution/0500-0599/0596.Classes More Than 5 Students/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ Each row of this table indicates the name of a student and the class in which th
1919

2020
<p>&nbsp;</p>
2121

22-
<p>Write an SQL query to report all the classes that have <strong>at least five students</strong>.</p>
22+
<p>Find all the classes that have <strong>at least five students</strong>.</p>
2323

2424
<p>Return the result table in <strong>any order</strong>.</p>
2525

26-
<p>The query result format is in the following example.</p>
26+
<p>The&nbsp;result format is in the following example.</p>
2727

2828
<p>&nbsp;</p>
2929
<p><strong class="example">Example 1:</strong></p>

solution/0600-0699/0607.Sales Person/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ Each row of this table contains information about one order. This includes the I
5858

5959
<p>&nbsp;</p>
6060

61-
<p>Write an SQL query to report the names of all the salespersons who did not have any orders related to the company with the name <strong>&quot;RED&quot;</strong>.</p>
61+
<p>Find the names of all the salespersons who did not have any orders related to the company with the name <strong>&quot;RED&quot;</strong>.</p>
6262

6363
<p>Return the result table in <strong>any order</strong>.</p>
6464

65-
<p>The query result format is in the following example.</p>
65+
<p>The result format is in the following example.</p>
6666

6767
<p>&nbsp;</p>
6868
<p><strong class="example">Example 1:</strong></p>

0 commit comments

Comments
 (0)