-
Notifications
You must be signed in to change notification settings - Fork 0
/
1507. Reformat Date 2.cpp
83 lines (68 loc) · 1.55 KB
/
1507. Reformat Date 2.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
72
73
74
75
76
77
78
79
80
81
82
83
// https://leetcode.com/problems/reformat-date/
class Solution
{
public:
string reformatDate(string date)
{
string result = getYear(date) + "-" + getMonth(date) + "-" + getDay(date);
return result;
}
string getDay(string &date)
{
string result = "";
int i = 0;
while (isdigit(date[i]))
{
result += date[i];
i++;
}
return mappingDay(result);
}
string mappingDay(string &day)
{
string result = (day.length() == 1) ? "0" + day : day;
return result;
}
string getMonth(string &date)
{
string result = "";
int i = 0;
while (date[i] != ' ')
i++;
i++;
while (date[i] != ' ')
{
result += date[i];
i++;
}
return mappingMonth(result);
}
string mappingMonth(string &month)
{
unordered_map<string, string> months = {
{"Jan", "01"},
{"Feb", "02"},
{"Mar", "03"},
{"Apr", "04"},
{"May", "05"},
{"Jun", "06"},
{"Jul", "07"},
{"Aug", "08"},
{"Sep", "09"},
{"Oct", "10"},
{"Nov", "11"},
{"Dec", "12"}};
return months[month];
}
string getYear(string &date)
{
string result = "";
int i = date.length() - 1;
while (date[i] != ' ')
{
result = date[i] + result;
i--;
}
return result;
}
};