[leetcode] 1185. Day of the Week

网友投稿 878 2022-08-22 15:30:13

[leetcode] 1185. Day of the Week

Description

Given a date, return the corresponding day of the week for that date.

The input is given as three integers representing the day, month and year respectively.

Return the answer as one of the following values {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}.

Example 1:

Input: day = 31, month = 8, year = 2019Output: "Saturday"

Example 2:

Input: day = 18, month = 7, year = 1999Output: "Sunday"

Example 3:

Input: day = 15, month = 8, year = 1993Output: "Sunday"

Constraints:

The given dates are valid dates between the years 1971 and 2100.

分析

题目的意思是:给定年月日,要算出是星期几,这道题本身可以调用python的一些包就可以解决,但是我感觉不是出这道题的重点,所以转向了通过计算判断事周几,首先要搞清楚闰年有几年,然后month有几天,day有几天,然后除以7取余数就行了。

代码

class Solution: def dayOfTheWeek(self, day: int, month: int, year: int) -> str: prev_year=year-1 days=prev_year*365+prev_year//4-prev_year//100+prev_year//400 days+=sum([31,28,31,30,31,30,31,31,30,31,30,31][:month-1]) days+=day if(month>2 and ((year%4==0 and year%100!=0) or (year%400==0))): days+=1 return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][days % 7]

参考文献

​​[LeetCode] Simple & Easy Solution by Python 3​​

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

上一篇:iOS:如何捕获异常?
下一篇:[leetcode] 1352. Product of the Last K Numbers
相关文章