LeetCode 1185. 一周中的第几天

LeetCode 1185. 一周中的第几天缩略图

给你一个日期,请你设计一个算法来判断它是对应一周中的哪一天。

输入为三个整数:day、month 和 year,分别表示日、月、年。

您返回的结果必须是这几个值中的一个 {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/day-of-the-week
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

LeetCode 1185. 一周中的第几天插图
OpenCV步步精深-可心科创工作室

思路:python都写好了,我直接拿来用

python3实现:

class Solution:
    def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
        # 直接调用datetime函数
        # 用给的day,month,year构造日期
        if len(str(day)) == 1:
            day = "0" + str(day)
        else:
            day = str(day)
        if len(str(month)) == 1:
            month = "0" + str(month)
        else:
            month = str(month)
        year = str(year)
        str_date = year + month + day

        list_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday"]
        result = datetime.datetime.strptime(str_date, "%Y%m%d").weekday() + 1

        return list_week[result-1]
LeetCode 1185. 一周中的第几天插图1
OpenCV步步精深-可心科创工作室

更多干货见 OpenCV步步精深