Skip to content

Commit 4af36dd

Browse files
authored
feat: add solutions to lc problem: No.3374 (#3831)
1 parent a2d4d2f commit 4af36dd

File tree

9 files changed

+322
-5
lines changed

9 files changed

+322
-5
lines changed

solution/0200-0299/0277.Find the Celebrity/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ tags:
2222

2323
<p>现在你想要确认这个 “名人” 是谁,或者确定这里没有&nbsp;“名人”。而你唯一能做的就是问诸如 “A&nbsp;你好呀,请问你认不认识&nbsp;B呀?”&nbsp;的问题,以确定 A 是否认识 B。你需要在(渐近意义上)尽可能少的问题内来确定这位 “名人” 是谁(或者确定这里没有 “名人”)。</p>
2424

25-
<p>在本题中,你可以使用辅助函数&nbsp;<code>bool knows(a, b)</code>&nbsp;获取到 A&nbsp;是否认识 B。请你来实现一个函数&nbsp;<code>int findCelebrity(n)</code>。</p>
25+
<p>给定整数&nbsp;<code>n</code>&nbsp;和一个辅助函数&nbsp;<code>bool knows(a, b)</code>&nbsp;用来获取&nbsp;<code>a</code> 是否认识&nbsp;<code>b</code>。实现一个函数&nbsp;<code>int findCelebrity(n)</code>。派对最多只会有一个 “名人” 参加。</p>
2626

27-
<p>派对最多只会有一个 “名人” 参加。若&nbsp;“名人” 存在,请返回他/她的编号;若&nbsp;“名人”&nbsp;不存在,请返回&nbsp;<code>-1</code>。</p>
27+
<p>若&nbsp;“名人” 存在,请返回他/她的编号;若&nbsp;“名人”&nbsp;不存在,请返回&nbsp;<code>-1</code>。</p>
28+
29+
<p><strong>注意</strong>&nbsp;<code>n x n</code>&nbsp;的二维数组&nbsp;<code>graph</code>&nbsp;给定的输入并不是&nbsp;<strong>直接</strong>&nbsp;提供给你的,而是&nbsp;<strong>只能</strong>&nbsp;通过辅助函数 <code>knows</code>&nbsp;获取。<code>graph[i][j] == 1</code>&nbsp;表示&nbsp;<code>i</code> 认识 <code>j</code>,而&nbsp;<code>graph[i][j] == 0</code>&nbsp;表示&nbsp;<code>j</code>&nbsp;不认识&nbsp;<code>i</code>。</p>
2830

2931
<p>&nbsp;</p>
3032

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ content_id 是这张表的唯一主键。
5656
+------------+-----------------------------------+
5757
| content_id | content_text |
5858
+------------+-----------------------------------+
59-
| 1 | hello world of SQL |
59+
| 1 | hello world of Sql |
6060
| 2 | the QUICK brown fox |
6161
| 3 | data science AND machine learning |
6262
| 4 | TOP rated programming BOOKS |
@@ -69,7 +69,7 @@ content_id 是这张表的唯一主键。
6969
+------------+-----------------------------------+-----------------------------------+
7070
| content_id | original_text | converted_text |
7171
+------------+-----------------------------------+-----------------------------------+
72-
| 1 | hello world of SQL | Hello World Of SQL |
72+
| 1 | hello world of Sql | Hello World Of Sql |
7373
| 2 | the QUICK brown fox | The Quick Brown Fox |
7474
| 3 | data science AND machine learning | Data Science And Machine Learning |
7575
| 4 | TOP rated programming BOOKS | Top Rated Programming Books |
@@ -81,7 +81,7 @@ content_id 是这张表的唯一主键。
8181
<ul>
8282
<li>对于 content_id = 1:
8383
<ul>
84-
<li>每个单词的首字母都已经大写:Hello World Of SQL</li>
84+
<li>每个单词的首字母都已经大写:Hello World Of Sql</li>
8585
</ul>
8686
</li>
8787
<li>对于 content_id = 2:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
comments: true
3+
difficulty: 困难
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README.md
5+
tags:
6+
- 数据库
7+
---
8+
9+
<!-- problem:start -->
10+
11+
# [3374. 首字母大写 II](https://leetcode.cn/problems/first-letter-capitalization-ii)
12+
13+
[English Version](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README_EN.md)
14+
15+
## 题目描述
16+
17+
<!-- description:start -->
18+
19+
<p>表:<code>user_content</code></p>
20+
21+
<pre>
22+
+-------------+---------+
23+
| Column Name | Type |
24+
+-------------+---------+
25+
| content_id | int |
26+
| content_text| varchar |
27+
+-------------+---------+
28+
content_id 是这张表的唯一主键。
29+
每一行包含一个不同的 ID 以及对应的文本内容。
30+
</pre>
31+
32+
<p>编写一个解决方案来根据下面的规则来转换&nbsp;<code>content_text</code>&nbsp;列中的文本:</p>
33+
34+
<ul>
35+
<li>将每个单词的 <strong>第一个字母</strong>&nbsp;转换为 <strong>大写</strong>,其余字母 <strong>保持小写</strong>。</li>
36+
<li>特殊处理包含特殊字符的单词:
37+
<ul>
38+
<li>对于用短横&nbsp;<code>-</code>&nbsp;连接的词语,<strong>两个部份</strong>&nbsp;都应该&nbsp;<strong>大写</strong>(<strong>例如</strong>,top-rated&nbsp;→ Top-Rated)</li>
39+
</ul>
40+
</li>
41+
<li>所有其他 <strong>格式</strong> 和 <strong>空格</strong> 应保持 <strong>不变</strong></li>
42+
</ul>
43+
44+
<p>返回结果表同时包含原始的&nbsp;<code>content_text</code> 以及根据上述规则修改后的文本。</p>
45+
46+
<p>结果格式如下例所示。</p>
47+
48+
<p>&nbsp;</p>
49+
50+
<p><strong class="example">示例:</strong></p>
51+
52+
<div class="example-block">
53+
<p><strong>输入:</strong></p>
54+
55+
<p>user_content 表:</p>
56+
57+
<pre class="example-io">
58+
+------------+---------------------------------+
59+
| content_id | content_text |
60+
+------------+---------------------------------+
61+
| 1 | hello world of SQL |
62+
| 2 | the QUICK-brown fox |
63+
| 3 | modern-day DATA science |
64+
| 4 | web-based FRONT-end development |
65+
+------------+---------------------------------+
66+
</pre>
67+
68+
<p><strong>输出:</strong></p>
69+
70+
<pre class="example-io">
71+
+------------+---------------------------------+---------------------------------+
72+
| content_id | original_text | converted_text |
73+
+------------+---------------------------------+---------------------------------+
74+
| 1 | hello world of SQL | Hello World Of Sql |
75+
| 2 | the QUICK-brown fox | The Quick-Brown Fox |
76+
| 3 | modern-day DATA science | Modern-Day Data Science |
77+
| 4 | web-based FRONT-end development | Web-Based Front-End Development |
78+
+------------+---------------------------------+---------------------------------+
79+
</pre>
80+
81+
<p><strong>解释:</strong></p>
82+
83+
<ul>
84+
<li>对于 content_id = 1:
85+
<ul>
86+
<li>每个单词的首字母都是大写的:"Hello World Of Sql"</li>
87+
</ul>
88+
</li>
89+
<li>对于 content_id = 2:
90+
<ul>
91+
<li>包含的连字符词 "QUICK-brown" 变为 "Quick-Brown"</li>
92+
<li>其它单词遵循普通的首字母大写规则</li>
93+
</ul>
94+
</li>
95+
<li>对于 content_id = 3:
96+
<ul>
97+
<li>连字符词 "modern-day" 变为 "Modern-Day"</li>
98+
<li>"DATA" 转换为 "Data"</li>
99+
</ul>
100+
</li>
101+
<li>对于 content_id = 4:
102+
<ul>
103+
<li>包含两个连字符词:"web-based" → "Web-Based"</li>
104+
<li>以及 "FRONT-end" → "Front-End"</li>
105+
</ul>
106+
</li>
107+
</ul>
108+
</div>
109+
110+
<!-- description:end -->
111+
112+
## 解法
113+
114+
<!-- solution:start -->
115+
116+
### 方法一
117+
118+
<!-- tabs:start -->
119+
120+
#### Pandas
121+
122+
```python
123+
import pandas as pd
124+
125+
126+
def capitalize_content(user_content: pd.DataFrame) -> pd.DataFrame:
127+
def convert_text(text: str) -> str:
128+
return " ".join(
129+
(
130+
"-".join([part.capitalize() for part in word.split("-")])
131+
if "-" in word
132+
else word.capitalize()
133+
)
134+
for word in text.split(" ")
135+
)
136+
137+
user_content["converted_text"] = user_content["content_text"].apply(convert_text)
138+
return user_content.rename(columns={"content_text": "original_text"})[
139+
["content_id", "original_text", "converted_text"]
140+
]
141+
```
142+
143+
<!-- tabs:end -->
144+
145+
<!-- solution:end -->
146+
147+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
comments: true
3+
difficulty: Hard
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README_EN.md
5+
tags:
6+
- Database
7+
---
8+
9+
<!-- problem:start -->
10+
11+
# [3374. First Letter Capitalization II](https://leetcode.com/problems/first-letter-capitalization-ii)
12+
13+
[中文文档](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README.md)
14+
15+
## Description
16+
17+
<!-- description:start -->
18+
19+
<p>Table: <code>user_content</code></p>
20+
21+
<pre>
22+
+-------------+---------+
23+
| Column Name | Type |
24+
+-------------+---------+
25+
| content_id | int |
26+
| content_text| varchar |
27+
+-------------+---------+
28+
content_id is the unique key for this table.
29+
Each row contains a unique ID and the corresponding text content.
30+
</pre>
31+
32+
<p>Write a solution to transform the text in the <code>content_text</code> column by applying the following rules:</p>
33+
34+
<ul>
35+
<li>Convert the <strong>first letter</strong> of each word to <strong>uppercase</strong> and the <strong>remaining</strong> letters to <strong>lowercase</strong></li>
36+
<li>Special handling for words containing special characters:
37+
<ul>
38+
<li>For words connected with a hyphen <code>-</code>, <strong>both parts</strong> should be <strong>capitalized</strong> (<strong>e.g.</strong>, top-rated&nbsp;&rarr; Top-Rated)</li>
39+
</ul>
40+
</li>
41+
<li>All other <strong>formatting</strong> and <strong>spacing</strong> should remain <strong>unchanged</strong></li>
42+
</ul>
43+
44+
<p>Return <em>the result table that includes both the original <code>content_text</code> and the modified text following the above rules</em>.</p>
45+
46+
<p>The result format is in the following example.</p>
47+
48+
<p>&nbsp;</p>
49+
<p><strong class="example">Example:</strong></p>
50+
51+
<div class="example-block">
52+
<p><strong>Input:</strong></p>
53+
54+
<p>user_content table:</p>
55+
56+
<pre class="example-io">
57+
+------------+---------------------------------+
58+
| content_id | content_text |
59+
+------------+---------------------------------+
60+
| 1 | hello world of SQL |
61+
| 2 | the QUICK-brown fox |
62+
| 3 | modern-day DATA science |
63+
| 4 | web-based FRONT-end development |
64+
+------------+---------------------------------+
65+
</pre>
66+
67+
<p><strong>Output:</strong></p>
68+
69+
<pre class="example-io">
70+
+------------+---------------------------------+---------------------------------+
71+
| content_id | original_text | converted_text |
72+
+------------+---------------------------------+---------------------------------+
73+
| 1 | hello world of SQL | Hello World Of Sql |
74+
| 2 | the QUICK-brown fox | The Quick-Brown Fox |
75+
| 3 | modern-day DATA science | Modern-Day Data Science |
76+
| 4 | web-based FRONT-end development | Web-Based Front-End Development |
77+
+------------+---------------------------------+---------------------------------+
78+
</pre>
79+
80+
<p><strong>Explanation:</strong></p>
81+
82+
<ul>
83+
<li>For content_id = 1:
84+
<ul>
85+
<li>Each word&#39;s first letter is capitalized: &quot;Hello World Of Sql&quot;</li>
86+
</ul>
87+
</li>
88+
<li>For content_id = 2:
89+
<ul>
90+
<li>Contains the hyphenated word &quot;QUICK-brown&quot; which becomes &quot;Quick-Brown&quot;</li>
91+
<li>Other words follow normal capitalization rules</li>
92+
</ul>
93+
</li>
94+
<li>For content_id = 3:
95+
<ul>
96+
<li>Hyphenated word &quot;modern-day&quot; becomes &quot;Modern-Day&quot;</li>
97+
<li>&quot;DATA&quot; is converted to &quot;Data&quot;</li>
98+
</ul>
99+
</li>
100+
<li>For content_id = 4:
101+
<ul>
102+
<li>Contains two hyphenated words: &quot;web-based&quot; &rarr; &quot;Web-Based&quot;</li>
103+
<li>And &quot;FRONT-end&quot; &rarr; &quot;Front-End&quot;</li>
104+
</ul>
105+
</li>
106+
</ul>
107+
</div>
108+
109+
<!-- description:end -->
110+
111+
## Solutions
112+
113+
<!-- solution:start -->
114+
115+
### Solution 1
116+
117+
<!-- tabs:start -->
118+
119+
#### Pandas
120+
121+
```python
122+
import pandas as pd
123+
124+
125+
def capitalize_content(user_content: pd.DataFrame) -> pd.DataFrame:
126+
def convert_text(text: str) -> str:
127+
return " ".join(
128+
(
129+
"-".join([part.capitalize() for part in word.split("-")])
130+
if "-" in word
131+
else word.capitalize()
132+
)
133+
for word in text.split(" ")
134+
)
135+
136+
user_content["converted_text"] = user_content["content_text"].apply(convert_text)
137+
return user_content.rename(columns={"content_text": "original_text"})[
138+
["content_id", "original_text", "converted_text"]
139+
]
140+
```
141+
142+
<!-- tabs:end -->
143+
144+
<!-- solution:end -->
145+
146+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pandas as pd
2+
3+
4+
def capitalize_content(user_content: pd.DataFrame) -> pd.DataFrame:
5+
def convert_text(text: str) -> str:
6+
return " ".join(
7+
(
8+
"-".join([part.capitalize() for part in word.split("-")])
9+
if "-" in word
10+
else word.capitalize()
11+
)
12+
for word in text.split(" ")
13+
)
14+
15+
user_content["converted_text"] = user_content["content_text"].apply(convert_text)
16+
return user_content.rename(columns={"content_text": "original_text"})[
17+
["content_id", "original_text", "converted_text"]
18+
]

solution/DATABASE_README.md

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@
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) | `数据库` | 简单 | 🔒 |
304304
| 3368 | [首字母大写](/solution/3300-3399/3368.First%20Letter%20Capitalization/README.md) | | 困难 | 🔒 |
305+
| 3374 | [首字母大写 II](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README.md) | | 困难 | |
305306

306307
## 版权
307308

solution/DATABASE_README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
300300
| 3338 | [Second Highest Salary II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README_EN.md) | `Database` | Medium | 🔒 |
301301
| 3358 | [Books with NULL Ratings](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README_EN.md) | `Database` | Easy | 🔒 |
302302
| 3368 | [First Letter Capitalization](/solution/3300-3399/3368.First%20Letter%20Capitalization/README_EN.md) | | Hard | 🔒 |
303+
| 3374 | [First Letter Capitalization II](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README_EN.md) | | Hard | |
303304

304305
## Copyright
305306

solution/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3383,6 +3383,7 @@
33833383
| 3371 | [识别数组中的最大异常值](/solution/3300-3399/3371.Identify%20the%20Largest%20Outlier%20in%20an%20Array/README.md) | | 中等 | 第 426 场周赛 |
33843384
| 3372 | [连接两棵树后最大目标节点数目 I](/solution/3300-3399/3372.Maximize%20the%20Number%20of%20Target%20Nodes%20After%20Connecting%20Trees%20I/README.md) | | 中等 | 第 426 场周赛 |
33853385
| 3373 | [连接两棵树后最大目标节点数目 II](/solution/3300-3399/3373.Maximize%20the%20Number%20of%20Target%20Nodes%20After%20Connecting%20Trees%20II/README.md) | | 困难 | 第 426 场周赛 |
3386+
| 3374 | [首字母大写 II](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README.md) | | 困难 | |
33863387

33873388
## 版权
33883389

solution/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -3381,6 +3381,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
33813381
| 3371 | [Identify the Largest Outlier in an Array](/solution/3300-3399/3371.Identify%20the%20Largest%20Outlier%20in%20an%20Array/README_EN.md) | | Medium | Weekly Contest 426 |
33823382
| 3372 | [Maximize the Number of Target Nodes After Connecting Trees I](/solution/3300-3399/3372.Maximize%20the%20Number%20of%20Target%20Nodes%20After%20Connecting%20Trees%20I/README_EN.md) | | Medium | Weekly Contest 426 |
33833383
| 3373 | [Maximize the Number of Target Nodes After Connecting Trees II](/solution/3300-3399/3373.Maximize%20the%20Number%20of%20Target%20Nodes%20After%20Connecting%20Trees%20II/README_EN.md) | | Hard | Weekly Contest 426 |
3384+
| 3374 | [First Letter Capitalization II](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README_EN.md) | | Hard | |
33843385

33853386
## Copyright
33863387

0 commit comments

Comments
 (0)