[leetcode] 1507. Reformat Date

网友投稿 908 2022-08-23

[leetcode] 1507. Reformat date

[leetcode] 1507. Reformat Date

Description

Given a date string in the form Day Month Year, where:

Day is in the set {“1st”, “2nd”, “3rd”, “4th”, …, “30th”, “31st”}.Month is in the set {“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”}.Year is in the range [1900, 2100].

Convert the date string to the format YYYY-MM-DD, where:

YYYY denotes the 4 digit year.MM denotes the 2 digit month.DD denotes the 2 digit day.

Example 1:

Input: date = "20th Oct 2052"Output: "2052-10-20"

Example 2:

Input: date = "6th Jun 1933"Output: "1933-06-06"

Example 3:

Input: date = "26th May 1960"Output: "1960-05-26"

Constraints:

The given dates are guaranteed to be valid, so no error handling is necessary.

分析

题目的意思是:把英文日期格式转换成用横线分隔符号的日期形式,我的想法很简单,就是字符串拼接了哈。

代码

class Solution: def reformatDate(self, date: str) -> str: months=["Jan","Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] day,month,year=date.split() m='' for i in range(len(months)): if(months[i]==month): m=str(i+1) d='' for ch in day: if(ch>='0' and ch<='9'): d+=ch if(int(d)<10): d='0'+d if(int(m)<10): m='0'+m return year+'-'+m+'-'+d

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python 开发的高级技巧(python是什么意思)
下一篇:[leetcode] 1247. Minimum Swaps to Make Strings Equal
相关文章

 发表评论

暂时没有评论,来抢沙发吧~