2129. 将标题首字母大写

将标题首字母大写

解法一: 模拟

go
1
2
3
4
5
6
7
8
9
10
11
func capitalizeTitle(title string) string {
fields := strings.Fields(title)
for i, field := range fields {
if len(field) <= 2 {
fields[i] = strings.ToLower(field)
} else {
fields[i] = strings.ToUpper(field[:1]) + strings.ToLower(field[1:])
}
}
return strings.Join(fields, " ")
}
java
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public String capitalizeTitle(String title) {
return Arrays.stream(title.split(" "))
.map(str -> {
if (str.length() <= 2) {
return str.toLowerCase();
}
return Character.toUpperCase(str.charAt(0)) + str.substring(1).toLowerCase();
})
.collect(Collectors.joining(" "));
}
}
作者

wuhunyu

发布于

2024-03-11

更新于

2025-01-15

许可协议