leetcode 937. 重新排列日志文件

题解

分别找出字母日志和数字日志,并对字母日志进行排序

示例代码(go)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func reorderLogFiles(logs []string) []string {
res := make([]string, 0)
letterRes := make([]string, 0)
digitRes := make([]string, 0)
letterMap := make(map[string]int)
for i, v := range logs {
splitArr := strings.Split(v, " ")
joinStr := strings.Join(splitArr[1:], "")
if strings.ContainsAny(joinStr, "0123456789") {
digitRes = append(digitRes, v)
continue
}
joinStr = strings.Join(splitArr[1:], " ")
letterMap[joinStr] = i
letterRes = append(letterRes, joinStr)
}
sort.Sort(sort.StringSlice(letterRes))
for _, v := range letterRes {
res = append(res, logs[letterMap[v]])
}
res = append(res, digitRes...)
return res
}