Python3
class Solution(object):
def reformatDate(self, date):
"""
:type date: str
:rtype: str
"""
month = {"Jan":"01", "Feb":"02", "Mar":"03", "Apr":"04", "May":"05", "Jun":"06", "Jul":"07", "Aug":"08", "Sep":"09", "Oct":"10", "Nov":"11", "Dec":"12"}
# date = date.split(" ")
# date[0] = date[0][:-2].zfill(2)
# date[1] = month.get(date[1])
# date.reverse()
# return "-".join(date)
str1 = date[-4:]
str2 = date[-8:-5]
str2 = month.get(str2)
str3 = date[:-11]
str3 = str3.zfill(2)
return str1+"-"+str2+"-"+str3
C++
class Solution {
public:
string reformatDate(string date) {
unordered_map<string, string> s2month = {
{"Jan", "01"},
{"Feb", "02"},
{"Mar", "03"},
{"Apr", "04"},
{"May", "05"},
{"Jun", "06"},
{"Jul", "07"},
{"Aug", "08"},
{"Sep", "09"},
{"Oct", "10"},
{"Nov", "11"},
{"Dec", "12"}
};
stringstream ss(date);
string year, month, day;
ss >> day >> month >> year;
month = s2month[month];
day.pop_back();
day.pop_back();
if (day.size() == 1) {
day = "0" + day;
}
return year + "-" + month + "-" + day;
}
};
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/reformat-date/solution/zhuan-bian-ri-qi-ge-shi-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
2021.03.12——力扣每日一题_1507.转变日期格式