Skip to content

Commit 11da429

Browse files
authored
feat: add ts solution to lc problem: No.2129 (#1360)
1 parent 6a8ce4b commit 11da429

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

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

+26-2
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,34 @@ func capitalizeTitle(title string) string {
133133

134134
### **TypeScript**
135135

136-
<!-- 这里可写当前语言的特殊实现逻辑 -->
137-
138136
```ts
137+
function capitalizeTitle(title: string): string {
138+
const ans: string[] = [];
139+
for (const s of title.split(' ')) {
140+
if (s.length < 3) {
141+
ans.push(s.toLowerCase());
142+
} else {
143+
ans.push(
144+
s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase(),
145+
);
146+
}
147+
}
148+
return ans.join(' ');
149+
}
150+
```
139151

152+
```ts
153+
function capitalizeTitle(title: string): string {
154+
return title
155+
.split(' ')
156+
.map(s =>
157+
s.length < 3
158+
? s.toLowerCase()
159+
: s.substring(0, 1).toUpperCase() +
160+
s.substring(1).toLowerCase(),
161+
)
162+
.join(' ');
163+
}
140164
```
141165

142166
### **...**

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

+26
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,33 @@ func capitalizeTitle(title string) string {
121121
### **TypeScript**
122122

123123
```ts
124+
function capitalizeTitle(title: string): string {
125+
const ans: string[] = [];
126+
for (const s of title.split(' ')) {
127+
if (s.length < 3) {
128+
ans.push(s.toLowerCase());
129+
} else {
130+
ans.push(
131+
s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase(),
132+
);
133+
}
134+
}
135+
return ans.join(' ');
136+
}
137+
```
124138

139+
```ts
140+
function capitalizeTitle(title: string): string {
141+
return title
142+
.split(' ')
143+
.map(s =>
144+
s.length < 3
145+
? s.toLowerCase()
146+
: s.substring(0, 1).toUpperCase() +
147+
s.substring(1).toLowerCase(),
148+
)
149+
.join(' ');
150+
}
125151
```
126152

127153
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function capitalizeTitle(title: string): string {
2+
const ans: string[] = [];
3+
for (const s of title.split(' ')) {
4+
if (s.length < 3) {
5+
ans.push(s.toLowerCase());
6+
} else {
7+
ans.push(
8+
s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase(),
9+
);
10+
}
11+
}
12+
return ans.join(' ');
13+
}

0 commit comments

Comments
 (0)