一.问题描述
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 = 2019 Output: "Saturday"
Example 2:
Input: day = 18, month = 7, year = 1999 Output: "Sunday"
Example 3:
Input: day = 15, month = 8, year = 1993 Output: "Sunday"
Constraints:
- The given dates are valid dates between the years
1971and2100.
二.解决思路
这道题我真的不知道咋说,要水也不能水成这个样子把。。。
主要就注意以下从啥时候开始计算
起始点是1971年1月1日,这天是周五
之后按一年年计算离给定日子总共有多少天,最后取余就好
涉及到闰年的判断:
闰年:年数能整除4, 当年数能整除100的时候还必须整除400,不然不算闰年。
更多leetcode算法题解法请关注我的专栏leetcode算法从零到结束或关注我
欢迎大家一起套路一起刷题一起ac
三.源码
class Solution:
def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
# 1971 1.1 is Friday
num2day={0:'Thursday',1:'Friday',2:'Saturday',3:'Sunday',4:'Monday',5:'Tuesday',6:'Wednesday'}
mon2num=[0,31,28,31,30,31,30,31,31,30,31,30,31]
count=0
for i in range(1971,year):
if i%400==0 or (i%4==0 and i%100!=0):
count+=366
else :
count+=365
if year%400==0 or (year%4==0 and year%100!=0):
mon2num[2]+=1
return num2day[(count+sum(mon2num[:month])+day)%7]
日期转星期算法解析
495

被折叠的 条评论
为什么被折叠?



