generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
71 lines (59 loc) · 1.06 KB
/
solution.cpp
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
#include <cstdint>
#include <string>
class Solution {
public:
std::string reformatDate(std::string date) {
auto c = date.data();
char d1{'0'}, d2{'0'};
if (c[1] > '9') {
d2 = c[0];
c += 4;
} else {
d1 = c[0];
d2 = c[1];
c += 5;
}
char m1{'0'}, m2{'0'};
switch (c[0]) {
case 'J':
m2 = c[1] == 'a' ? '1' : (c[2] == 'n' ? '6' : '7');
break;
case 'F':
m2 = '2';
break;
case 'M':
m2 = c[2] == 'r' ? '3' : '5';
break;
case 'A':
m2 = c[1] == 'p' ? '4' : '8';
break;
case 'S':
m2 = '9';
break;
case 'O':
m1 = '1';
break;
case 'N':
m1 = '1';
m2 = '1';
break;
case 'D':
m1 = '1';
m2 = '2';
break;
}
auto s = date.data();
s[0] = c[4];
s[1] = c[5];
s[2] = c[6];
s[3] = c[7];
s[4] = '-';
s[5] = m1;
s[6] = m2;
s[7] = '-';
s[8] = d1;
s[9] = d2;
date.resize(10);
return date;
}
};