-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution1507.go
More file actions
59 lines (53 loc) · 1.14 KB
/
solution1507.go
File metadata and controls
59 lines (53 loc) · 1.14 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
package solution1507
import (
"fmt"
"strings"
)
// ============================================================================
// 1507. Reformat Date
// URL: https://leetcode.com/problems/reformat-date/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_reformatDate-24 2624359 477.9 ns/op 714 B/op 7 allocs/op
PASS
*/
func reformatDate(date string) string {
sb := strings.Builder{}
month := map[string]string{
"Jan": "01",
"Feb": "02",
"Mar": "03",
"Apr": "04",
"May": "05",
"Jun": "06",
"Jul": "07",
"Aug": "08",
"Sep": "09",
"Oct": "10",
"Nov": "11",
"Dec": "12",
}
parts := strings.Fields(date)
sb.WriteString(parts[2])
sb.WriteString("-")
v, ok := month[parts[1]]
if !ok {
panic("value not found!")
}
sb.WriteString(v)
sb.WriteString("-")
num := parts[0]
switch {
case 'a' <= num[1] && num[1] <= 'z':
num = num[:1]
case 'a' <= num[2] && num[2] <= 'z':
num = num[:2]
}
num = fmt.Sprintf("%02s", num)
sb.WriteString(num)
return sb.String()
}