Skip to content

Commit aea6a2e

Browse files
committed
Runtime: 0 ms (Top 100.0%) | Memory: 6.50 MB (Top 57.94%)
1 parent dc22aa1 commit aea6a2e

File tree

1 file changed

+16
-41
lines changed

1 file changed

+16
-41
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,20 @@
1+
// Runtime: 0 ms (Top 100.0%) | Memory: 6.50 MB (Top 57.94%)
2+
13
class Solution {
24
public:
35
string reformatDate(string date) {
4-
map<string,int>m;
5-
m["Jan"] =1;
6-
m["Feb"] =2;
7-
m["Mar"] =3;
8-
m["Apr"] =4;
9-
m["May"] =5;
10-
m["Jun"] =6;
11-
m["Jul"] =7;
12-
m["Aug"] =8;
13-
m["Sep"] =9;
14-
m["Oct"] =10;
15-
m["Nov"] =11;
16-
m["Dec"] =12;
17-
string ans;
18-
for(int i=date.length()-4; i<date.length(); i++){
19-
ans+=date[i];
20-
}
21-
ans += "-";
22-
string month;
23-
for(int i=date.length()-8; i<=date.length()-6; i++){
24-
month+=date[i];
25-
}
26-
27-
int yes = m[month];
28-
29-
if(yes<=9){
30-
ans += '0';
31-
}
32-
ans = ans + to_string(yes);
33-
ans += "-";
34-
int i = 0;
35-
if(date[1]=='t' || date[1]=='s' || date[1]=='n' || date[1]=='r'){
36-
ans+='0';
37-
ans+=date[0];
38-
}
39-
else{
40-
ans+=date[0];
41-
ans+=date[1];
42-
}
43-
return ans;
6+
// easiest way to get index with O(1)
7+
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"}};
8+
istringstream s(date);
9+
// get day
10+
string day; s >> day;
11+
day.pop_back(); day.pop_back();
12+
if (day.size() == 1)
13+
day = "0" + day;
14+
// get month
15+
string month; s >> month;
16+
// get year
17+
string year; s >> year;
18+
return year + "-" + months[month] + "-" + day;
4419
}
45-
};
20+
};

0 commit comments

Comments
 (0)