Skip to content

Commit e7b9f2f

Browse files
committed
feat: add solutions to lc problem: No.2129
No.2129.Capitalize the Title
1 parent 50649c7 commit e7b9f2f

File tree

6 files changed

+150
-2
lines changed

6 files changed

+150
-2
lines changed

solution/2100-2199/2129.Capitalize the Title/README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,78 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60+
**方法一:模拟**
61+
62+
直接模拟,按空格切分字符串,得到每个单词,再按题目转大小写。最后用空格连接每个单词。
63+
64+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 `title` 的长度。
65+
6066
<!-- tabs:start -->
6167

6268
### **Python3**
6369

6470
<!-- 这里可写当前语言的特殊实现逻辑 -->
6571

6672
```python
67-
73+
class Solution:
74+
def capitalizeTitle(self, title: str) -> str:
75+
words = [w.lower() if len(w) < 3 else w.capitalize() for w in title.split()]
76+
return " ".join(words)
6877
```
6978

7079
### **Java**
7180

7281
<!-- 这里可写当前语言的特殊实现逻辑 -->
7382

7483
```java
84+
class Solution {
85+
public String capitalizeTitle(String title) {
86+
List<String> ans = new ArrayList<>();
87+
for (String s : title.split(" ")) {
88+
if (s.length() < 3) {
89+
ans.add(s.toLowerCase());
90+
} else {
91+
ans.add(s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
92+
}
93+
}
94+
return String.join(" ", ans);
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
string capitalizeTitle(string title) {
105+
transform(title.begin(), title.end(), title.begin(), ::tolower);
106+
istringstream ss(title);
107+
string ans;
108+
while (ss >> title) {
109+
if (title.size() > 2) title[0] = toupper(title[0]);
110+
ans += title;
111+
ans += " ";
112+
}
113+
ans.pop_back();
114+
return ans;
115+
}
116+
};
117+
```
75118
119+
### **Go**
120+
121+
```go
122+
func capitalizeTitle(title string) string {
123+
title = strings.ToLower(title)
124+
words := strings.Split(title, " ")
125+
for i, s := range words {
126+
if len(s) > 2 {
127+
words[i] = strings.Title(s)
128+
}
129+
}
130+
return strings.Join(words, " ")
131+
}
76132
```
77133

78134
### **TypeScript**

solution/2100-2199/2129.Capitalize the Title/README_EN.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,63 @@ The remaining words have a length of at least 3, so the first letter of each rem
5959
### **Python3**
6060

6161
```python
62-
62+
class Solution:
63+
def capitalizeTitle(self, title: str) -> str:
64+
words = [w.lower() if len(w) < 3 else w.capitalize() for w in title.split()]
65+
return " ".join(words)
6366
```
6467

6568
### **Java**
6669

6770
```java
71+
class Solution {
72+
public String capitalizeTitle(String title) {
73+
List<String> ans = new ArrayList<>();
74+
for (String s : title.split(" ")) {
75+
if (s.length() < 3) {
76+
ans.add(s.toLowerCase());
77+
} else {
78+
ans.add(s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
79+
}
80+
}
81+
return String.join(" ", ans);
82+
}
83+
}
84+
```
85+
86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
string capitalizeTitle(string title) {
92+
transform(title.begin(), title.end(), title.begin(), ::tolower);
93+
istringstream ss(title);
94+
string ans;
95+
while (ss >> title) {
96+
if (title.size() > 2) title[0] = toupper(title[0]);
97+
ans += title;
98+
ans += " ";
99+
}
100+
ans.pop_back();
101+
return ans;
102+
}
103+
};
104+
```
68105
106+
### **Go**
107+
108+
```go
109+
func capitalizeTitle(title string) string {
110+
title = strings.ToLower(title)
111+
words := strings.Split(title, " ")
112+
for i, s := range words {
113+
if len(s) > 2 {
114+
words[i] = strings.Title(s)
115+
}
116+
}
117+
return strings.Join(words, " ")
118+
}
69119
```
70120

71121
### **TypeScript**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
string capitalizeTitle(string title) {
4+
transform(title.begin(), title.end(), title.begin(), ::tolower);
5+
istringstream ss(title);
6+
string ans;
7+
while (ss >> title) {
8+
if (title.size() > 2) title[0] = toupper(title[0]);
9+
ans += title;
10+
ans += " ";
11+
}
12+
ans.pop_back();
13+
return ans;
14+
}
15+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func capitalizeTitle(title string) string {
2+
title = strings.ToLower(title)
3+
words := strings.Split(title, " ")
4+
for i, s := range words {
5+
if len(s) > 2 {
6+
words[i] = strings.Title(s)
7+
}
8+
}
9+
return strings.Join(words, " ")
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public String capitalizeTitle(String title) {
3+
List<String> ans = new ArrayList<>();
4+
for (String s : title.split(" ")) {
5+
if (s.length() < 3) {
6+
ans.add(s.toLowerCase());
7+
} else {
8+
ans.add(s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
9+
}
10+
}
11+
return String.join(" ", ans);
12+
}
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def capitalizeTitle(self, title: str) -> str:
3+
words = [w.lower() if len(w) < 3 else w.capitalize() for w in title.split()]
4+
return " ".join(words)

0 commit comments

Comments
 (0)