-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2129-CapitalizeTheTitle.go
More file actions
94 lines (83 loc) · 3.64 KB
/
2129-CapitalizeTheTitle.go
File metadata and controls
94 lines (83 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
// 2129. Capitalize the Title
// You are given a string title consisting of one or more words separated by a single space, where each word consists of English letters.
// Capitalize the string by changing the capitalization of each word such that:
// If the length of the word is 1 or 2 letters, change all letters to lowercase.
// Otherwise, change the first letter to uppercase and the remaining letters to lowercase.
// Return the capitalized title.
// Example 1:
// Input: title = "capiTalIze tHe titLe"
// Output: "Capitalize The Title"
// Explanation:
// Since all the words have a length of at least 3, the first letter of each word is uppercase, and the remaining letters are lowercase.
// Example 2:
// Input: title = "First leTTeR of EACH Word"
// Output: "First Letter of Each Word"
// Explanation:
// The word "of" has length 2, so it is all lowercase.
// The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
// Example 3:
// Input: title = "i lOve leetcode"
// Output: "i Love Leetcode"
// Explanation:
// The word "i" has length 1, so it is lowercase.
// The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
// Constraints:
// 1 <= title.length <= 100
// title consists of words separated by a single space without any leading or trailing spaces.
// Each word consists of uppercase and lowercase English letters and is non-empty.
import "fmt"
import "strings"
func capitalizeTitle1(title string) string {
l := len(title)
res := make([]byte, l)
flag := false // 需要大写
for i := 0; i < l; i++ {
res[i] = title[i]
// 处理开头 只有 1-2 字符的大小写的问题
if i == 0 {
if (i + 1 < l && title[i + 1] == ' ') || (i + 2 < l && title[i + 2] == ' ') {
} else {
flag = true
}
}
// 遇到了空格下一个就要大写
if title[i] == ' ' {
// 判段只有1-2个字符的单词开头不需要转成大写
if (i + 2 < l && title[i + 2] == ' ') || (i + 3 < l && title[i + 3] == ' ') {
} else {
flag = true
continue
}
}
if flag && (title[i] >= 'a' && title[i] <= 'z') { // 判断单词开头是否小写
res[i] = title[i] - 32
} else if !flag && title[i] >= 'A' && title[i] <= 'Z' { // 判断单词其它部份(除了开头)是否大写
res[i] = title[i] + 32
}
flag = false
}
return string(res)
}
func capitalizeTitle(title string) string {
title = strings.ToLower(title)
v := strings.Split(title, " ")
for i := 0; i < len(v); i++ {
// 如果单词的长度为 1 或者 2 ,所有字母变成小写。
if len(v[i]) < 3 {
v[i] = strings.ToLower(v[i])
} else { // 否则,将单词首字母大写,剩余字母变成小写。
v[i] = strings.Title(v[i])
}
}
title = strings.Join(v, " ")
return title
}
func main() {
fmt.Println(capitalizeTitle("capiTalIze tHe titLe")) // capiTalIze tHe titLe
fmt.Println(capitalizeTitle("First leTTeR of EACH Word")) // First Letter of Each Word
fmt.Println(capitalizeTitle("i lOve leetcode")) // i Love Leetcode
fmt.Println(capitalizeTitle1("capiTalIze tHe titLe")) // capiTalIze tHe titLe
fmt.Println(capitalizeTitle1("First leTTeR of EACH Word")) // First Letter of Each Word
fmt.Println(capitalizeTitle1("i lOve leetcode")) // i Love Leetcode
}