Skip to content

Commit 7e4cfbf

Browse files
authored
feat: add solutions to lc problem: No.3368 (#3813)
No.3368.First Letter Capitalization
1 parent 78e0f6b commit 7e4cfbf

File tree

8 files changed

+204
-51
lines changed

8 files changed

+204
-51
lines changed

solution/3300-3399/3355.Zero Array Transformation I/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@ tags:
2222
<p>对于每个查询&nbsp;<code>queries[i]</code>:</p>
2323

2424
<ul>
25-
<li>在&nbsp;<code>nums</code>&nbsp;的下标范围&nbsp;<code>[l<sub>i</sub>, r<sub>i</sub>]</code>&nbsp;内选择一个下标子集。</li>
25+
<li>在&nbsp;<code>nums</code>&nbsp;的下标范围&nbsp;<code>[l<sub>i</sub>, r<sub>i</sub>]</code>&nbsp;内选择一个下标 <span data-keyword="subset">子集</span>。</li>
2626
<li>将选中的每个下标对应的元素值减 1。</li>
2727
</ul>
2828

2929
<p><strong>零数组&nbsp;</strong>是指所有元素都等于 0 的数组。</p>
3030

3131
<p>如果在按顺序处理所有查询后,可以将 <code>nums</code> 转换为&nbsp;<strong>零数组&nbsp;</strong>,则返回 <code>true</code>,否则返回 <code>false</code>。</p>
3232

33-
<p>数组的&nbsp;<strong>子集&nbsp;</strong>是对数组元素的选择(可能为空)。</p>
34-
3533
<p>&nbsp;</p>
3634

3735
<p><strong class="example">示例 1:</strong></p>

solution/3300-3399/3359.Find Sorted Submatrices With Maximum Element at Most K/README.md

+22-20
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,39 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3359.Fi
66

77
<!-- problem:start -->
88

9-
# [3359. Find Sorted Submatrices With Maximum Element at Most K 🔒](https://leetcode.cn/problems/find-sorted-submatrices-with-maximum-element-at-most-k)
9+
# [3359. 查找最大元素不超过 K 的有序子矩阵 🔒](https://leetcode.cn/problems/find-sorted-submatrices-with-maximum-element-at-most-k)
1010

1111
[English Version](/solution/3300-3399/3359.Find%20Sorted%20Submatrices%20With%20Maximum%20Element%20at%20Most%20K/README_EN.md)
1212

1313
## 题目描述
1414

1515
<!-- description:start -->
1616

17-
<p>You are given a 2D matrix <code>grid</code> of size <code>m x n</code>. You are also given a <strong>non-negative</strong> integer <code>k</code>.</p>
17+
<p>给定一个大小为&nbsp;<code>m x n</code>&nbsp;的二维矩阵&nbsp;<code>grid</code>。同时给定一个 <strong>非负整数</strong>&nbsp;<code>k</code></p>
1818

19-
<p>Return the number of <strong>submatrices</strong> of <code>grid</code> that satisfy the following conditions:</p>
19+
<p>返回满足下列条件的&nbsp;<code>grid</code>&nbsp;的子矩阵:</p>
2020

2121
<ul>
22-
<li>The maximum element in the submatrix <strong>less than or equal to</strong> <code>k</code>.</li>
23-
<li>Each row in the submatrix is sorted in <strong>non-increasing</strong> order.</li>
22+
<li>子矩阵中最大的元素 <b>小于等于</b>&nbsp;<code>k</code></li>
23+
<li>子矩阵的每一行都以 <strong>非递增</strong> 顺序排序。</li>
2424
</ul>
2525

26-
<p>A submatrix <code>(x1, y1, x2, y2)</code> is a matrix that forms by choosing all cells <code>grid[x][y]</code> where <code>x1 &lt;= x &lt;= x2</code> and <code>y1 &lt;= y &lt;= y2</code>.</p>
26+
<p>矩阵的子矩阵&nbsp;<code>(x1, y1, x2, y2)</code>&nbsp;是通过选择所有满足&nbsp;<code>x1 &lt;= x &lt;= x2</code>&nbsp;&nbsp;<code>y1 &lt;= y &lt;= y2</code>&nbsp;&nbsp;<code>grid[x][y]</code> 元素组成的矩阵。</p>
2727

2828
<p>&nbsp;</p>
29-
<p><strong class="example">Example 1:</strong></p>
29+
30+
<p><strong class="example">示例 1:</strong></p>
3031

3132
<div class="example-block">
32-
<p><strong>Input:</strong> <span class="example-io">grid = [[4,3,2,1],[8,7,6,1]], k = 3</span></p>
33+
<p><span class="example-io"><b>输入:</b>grid = [[4,3,2,1],[8,7,6,1]], k = 3</span></p>
3334

34-
<p><strong>Output:</strong> <span class="example-io">8</span></p>
35+
<p><strong>输出:</strong><span class="example-io">8</span></p>
3536

36-
<p><strong>Explanation:</strong></p>
37+
<p><strong>解释:</strong></p>
3738

3839
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3300-3399/3359.Find%20Sorted%20Submatrices%20With%20Maximum%20Element%20at%20Most%20K/images/mine.png" style="width: 360px; height: 200px;" /></strong></p>
3940

40-
<p>The 8 submatrices are:</p>
41+
<p>8 个子矩阵分别是:</p>
4142

4243
<ul>
4344
<li><code>[[1]]</code></li>
@@ -51,28 +52,29 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3359.Fi
5152
</ul>
5253
</div>
5354

54-
<p><strong class="example">Example 2:</strong></p>
55+
<p><strong class="example">示例 2:</strong></p>
5556

5657
<div class="example-block">
57-
<p><strong>Input:</strong> <span class="example-io">grid = [[1,1,1],[1,1,1],[1,1,1]], k = 1</span></p>
58+
<p><span class="example-io"><b>输入:</b>grid = [[1,1,1],[1,1,1],[1,1,1]], k = 1</span></p>
5859

59-
<p><strong>Output:</strong> <span class="example-io">36</span></p>
60+
<p><span class="example-io"><b>输出:</b>36</span></p>
6061

61-
<p><strong>Explanation:</strong></p>
62+
<p><strong>解释:</strong></p>
6263

63-
<p>There are 36 submatrices of grid. All submatrices have their maximum element equal to 1.</p>
64+
<p>矩阵中有 36 个子矩阵。所有子矩阵的最大元素都等于 1。</p>
6465
</div>
6566

66-
<p><strong class="example">Example 3:</strong></p>
67+
<p><strong class="example">示例 3:</strong></p>
6768

6869
<div class="example-block">
69-
<p><strong>Input:</strong> <span class="example-io">grid = [[1]], k = 1</span></p>
70+
<p><span class="example-io"><b>输入:</b>grid = [[1]], k = 1</span></p>
7071

71-
<p><strong>Output:</strong> <span class="example-io">1</span></p>
72+
<p><span class="example-io"><b>输出:</b>1</span></p>
7273
</div>
7374

7475
<p>&nbsp;</p>
75-
<p><strong>Constraints:</strong></p>
76+
77+
<p><strong>提示:</strong></p>
7678

7779
<ul>
7880
<li><code>1 &lt;= m == grid.length &lt;= 10<sup>3</sup></code></li>

solution/3300-3399/3368.First Letter Capitalization/README.md

+78-25
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ tags:
88

99
<!-- problem:start -->
1010

11-
# [3368. First Letter Capitalization 🔒](https://leetcode.cn/problems/first-letter-capitalization)
11+
# [3368. 首字母大写 🔒](https://leetcode.cn/problems/first-letter-capitalization)
1212

1313
[English Version](/solution/3300-3399/3368.First%20Letter%20Capitalization/README_EN.md)
1414

1515
## 题目描述
1616

1717
<!-- description:start -->
1818

19-
<p>Table: <code>user_content</code></p>
19+
<p>表:<code>user_content</code></p>
2020

2121
<pre>
2222
+-------------+---------+
@@ -25,31 +25,32 @@ tags:
2525
| content_id | int |
2626
| content_text| varchar |
2727
+-------------+---------+
28-
content_id is the unique key for this table.
29-
Each row contains a unique ID and the corresponding text content.
28+
content_id 是这张表的唯一主键。
29+
每一行包含一个不同的 ID 以及对应的文本内容。
3030
</pre>
3131

32-
<p>Write a solution to transform the text in the <code>content_text</code> column by applying the following rules:</p>
32+
<p>编写一个解决方案来通过应用以下规则来转换&nbsp;<code>content_text</code>&nbsp;列中的文本:</p>
3333

3434
<ul>
35-
<li>Convert the first letter of each word to uppercase</li>
36-
<li>Keep all other letters in lowercase</li>
37-
<li>Preserve all existing spaces</li>
35+
<li>把每个单词的首字母变成大写</li>
36+
<li>其它字母保持小写</li>
37+
<li>保留所有现有空格</li>
3838
</ul>
3939

40-
<p><strong>Note</strong>: There will be no special character in <code>content_text</code>.</p>
40+
<p><b>注意:</b><code>content_text</code>&nbsp;中没有特殊字符。</p>
4141

42-
<p>Return <em>the result table that includes both the original <code>content_text</code> and the modified text where each word starts with a capital letter</em>.</p>
42+
<p>返回结果表,同时包含原来的<em>&nbsp;<code>content_text</code>&nbsp;</em>以及将所有单词首字母变成大写的修改后文本。</p>
4343

44-
<p>The result format is in the following example.</p>
44+
<p>结果格式如下所示。</p>
4545

4646
<p>&nbsp;</p>
47-
<p><strong class="example">Example:</strong></p>
47+
48+
<p><strong class="example">示例:</strong></p>
4849

4950
<div class="example-block">
50-
<p><strong>Input:</strong></p>
51+
<p><strong>输入:</strong></p>
5152

52-
<p>user_content table:</p>
53+
<p>user_content 表:</p>
5354

5455
<pre class="example-io">
5556
+------------+-----------------------------------+
@@ -62,7 +63,7 @@ Each row contains a unique ID and the corresponding text content.
6263
+------------+-----------------------------------+
6364
</pre>
6465

65-
<p><strong>Output:</strong></p>
66+
<p><strong>输出:</strong></p>
6667

6768
<pre class="example-io">
6869
+------------+-----------------------------------+-----------------------------------+
@@ -75,28 +76,28 @@ Each row contains a unique ID and the corresponding text content.
7576
+------------+-----------------------------------+-----------------------------------+
7677
</pre>
7778

78-
<p><strong>Explanation:</strong></p>
79+
<p><strong>解释:</strong></p>
7980

8081
<ul>
81-
<li>For content_id = 1:
82+
<li>对于 content_id = 1:
8283
<ul>
83-
<li>Each word&#39;s first letter is capitalized: Hello World Of SQL</li>
84+
<li>每个单词的首字母都已经大写:Hello World Of SQL</li>
8485
</ul>
8586
</li>
86-
<li>For content_id = 2:
87+
<li>对于 content_id = 2:
8788
<ul>
88-
<li>Original mixed-case text is transformed to title case: The Quick Brown Fox</li>
89+
<li>原来混合大小写的文本变为首字母大写:The Quick Brown Fox</li>
8990
</ul>
9091
</li>
91-
<li>For content_id = 3:
92+
<li>对于 content_id = 3:
9293
<ul>
93-
<li>The word AND&nbsp;is converted to &quot;And&quot;: &quot;Data Science And Machine Learning&quot;</li>
94+
<li>单词 AND 被转换为 "And":"Data Science And Machine Learning"</li>
9495
</ul>
9596
</li>
96-
<li>For content_id = 4:
97+
<li>对于 content_id = 4:
9798
<ul>
98-
<li>Handles&nbsp;word TOP rated&nbsp;correctly: Top Rated</li>
99-
<li>Converts BOOKS&nbsp;from all caps to title case: Books</li>
99+
<li>正确处理单词 TOP ratedTop Rated</li>
100+
<li> BOOKS 从全大写改为首字母大写:Books</li>
100101
</ul>
101102
</li>
102103
</ul>
@@ -115,7 +116,59 @@ Each row contains a unique ID and the corresponding text content.
115116
#### MySQL
116117

117118
```sql
119+
WITH RECURSIVE
120+
capitalized_words AS (
121+
SELECT
122+
content_id,
123+
content_text,
124+
SUBSTRING_INDEX(content_text, ' ', 1) AS word,
125+
SUBSTRING(
126+
content_text,
127+
LENGTH(SUBSTRING_INDEX(content_text, ' ', 1)) + 2
128+
) AS remaining_text,
129+
CONCAT(
130+
UPPER(LEFT(SUBSTRING_INDEX(content_text, ' ', 1), 1)),
131+
LOWER(SUBSTRING(SUBSTRING_INDEX(content_text, ' ', 1), 2))
132+
) AS processed_word
133+
FROM user_content
134+
UNION ALL
135+
SELECT
136+
c.content_id,
137+
c.content_text,
138+
SUBSTRING_INDEX(c.remaining_text, ' ', 1),
139+
SUBSTRING(c.remaining_text, LENGTH(SUBSTRING_INDEX(c.remaining_text, ' ', 1)) + 2),
140+
CONCAT(
141+
c.processed_word,
142+
' ',
143+
CONCAT(
144+
UPPER(LEFT(SUBSTRING_INDEX(c.remaining_text, ' ', 1), 1)),
145+
LOWER(SUBSTRING(SUBSTRING_INDEX(c.remaining_text, ' ', 1), 2))
146+
)
147+
)
148+
FROM capitalized_words c
149+
WHERE c.remaining_text != ''
150+
)
151+
SELECT
152+
content_id,
153+
content_text AS original_text,
154+
MAX(processed_word) AS converted_text
155+
FROM capitalized_words
156+
GROUP BY 1, 2;
157+
```
158+
159+
#### Pandas
160+
161+
```python
162+
import pandas as pd
163+
118164

165+
def process_text(user_content: pd.DataFrame) -> pd.DataFrame:
166+
user_content["converted_text"] = user_content["content_text"].apply(
167+
lambda text: " ".join(word.capitalize() for word in text.split(" "))
168+
)
169+
return user_content[["content_id", "content_text", "converted_text"]].rename(
170+
columns={"content_text": "original_text"}
171+
)
119172
```
120173

121174
<!-- tabs:end -->

solution/3300-3399/3368.First Letter Capitalization/README_EN.md

+52
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,59 @@ Each row contains a unique ID and the corresponding text content.
115115
#### MySQL
116116

117117
```sql
118+
WITH RECURSIVE
119+
capitalized_words AS (
120+
SELECT
121+
content_id,
122+
content_text,
123+
SUBSTRING_INDEX(content_text, ' ', 1) AS word,
124+
SUBSTRING(
125+
content_text,
126+
LENGTH(SUBSTRING_INDEX(content_text, ' ', 1)) + 2
127+
) AS remaining_text,
128+
CONCAT(
129+
UPPER(LEFT(SUBSTRING_INDEX(content_text, ' ', 1), 1)),
130+
LOWER(SUBSTRING(SUBSTRING_INDEX(content_text, ' ', 1), 2))
131+
) AS processed_word
132+
FROM user_content
133+
UNION ALL
134+
SELECT
135+
c.content_id,
136+
c.content_text,
137+
SUBSTRING_INDEX(c.remaining_text, ' ', 1),
138+
SUBSTRING(c.remaining_text, LENGTH(SUBSTRING_INDEX(c.remaining_text, ' ', 1)) + 2),
139+
CONCAT(
140+
c.processed_word,
141+
' ',
142+
CONCAT(
143+
UPPER(LEFT(SUBSTRING_INDEX(c.remaining_text, ' ', 1), 1)),
144+
LOWER(SUBSTRING(SUBSTRING_INDEX(c.remaining_text, ' ', 1), 2))
145+
)
146+
)
147+
FROM capitalized_words c
148+
WHERE c.remaining_text != ''
149+
)
150+
SELECT
151+
content_id,
152+
content_text AS original_text,
153+
MAX(processed_word) AS converted_text
154+
FROM capitalized_words
155+
GROUP BY 1, 2;
156+
```
157+
158+
#### Pandas
159+
160+
```python
161+
import pandas as pd
162+
118163

164+
def process_text(user_content: pd.DataFrame) -> pd.DataFrame:
165+
user_content["converted_text"] = user_content["content_text"].apply(
166+
lambda text: " ".join(word.capitalize() for word in text.split(" "))
167+
)
168+
return user_content[["content_id", "content_text", "converted_text"]].rename(
169+
columns={"content_text": "original_text"}
170+
)
119171
```
120172

121173
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pandas as pd
2+
3+
4+
def process_text(user_content: pd.DataFrame) -> pd.DataFrame:
5+
user_content["converted_text"] = user_content["content_text"].apply(
6+
lambda text: " ".join(word.capitalize() for word in text.split(" "))
7+
)
8+
return user_content[["content_id", "content_text", "converted_text"]].rename(
9+
columns={"content_text": "original_text"}
10+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
WITH RECURSIVE
2+
capitalized_words AS (
3+
SELECT
4+
content_id,
5+
content_text,
6+
SUBSTRING_INDEX(content_text, ' ', 1) AS word,
7+
SUBSTRING(
8+
content_text,
9+
LENGTH(SUBSTRING_INDEX(content_text, ' ', 1)) + 2
10+
) AS remaining_text,
11+
CONCAT(
12+
UPPER(LEFT(SUBSTRING_INDEX(content_text, ' ', 1), 1)),
13+
LOWER(SUBSTRING(SUBSTRING_INDEX(content_text, ' ', 1), 2))
14+
) AS processed_word
15+
FROM user_content
16+
UNION ALL
17+
SELECT
18+
c.content_id,
19+
c.content_text,
20+
SUBSTRING_INDEX(c.remaining_text, ' ', 1),
21+
SUBSTRING(c.remaining_text, LENGTH(SUBSTRING_INDEX(c.remaining_text, ' ', 1)) + 2),
22+
CONCAT(
23+
c.processed_word,
24+
' ',
25+
CONCAT(
26+
UPPER(LEFT(SUBSTRING_INDEX(c.remaining_text, ' ', 1), 1)),
27+
LOWER(SUBSTRING(SUBSTRING_INDEX(c.remaining_text, ' ', 1), 2))
28+
)
29+
)
30+
FROM capitalized_words c
31+
WHERE c.remaining_text != ''
32+
)
33+
SELECT
34+
content_id,
35+
content_text AS original_text,
36+
MAX(processed_word) AS converted_text
37+
FROM capitalized_words
38+
GROUP BY 1, 2;

solution/DATABASE_README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@
301301
| 3328 | [查找每个州的城市 II](/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README.md) | `数据库` | 中等 | 🔒 |
302302
| 3338 | [第二高的薪水 II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md) | `数据库` | 中等 | 🔒 |
303303
| 3358 | [评分为 NULL 的图书](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README.md) | `数据库` | 简单 | 🔒 |
304-
| 3368 | [First Letter Capitalization](/solution/3300-3399/3368.First%20Letter%20Capitalization/README.md) | | 困难 | 🔒 |
304+
| 3368 | [首字母大写](/solution/3300-3399/3368.First%20Letter%20Capitalization/README.md) | | 困难 | 🔒 |
305305

306306
## 版权
307307

0 commit comments

Comments
 (0)