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 1971 and 2100.
Solution in python:
class Solution:
def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
def isYear(year):
if ((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0):
return True
else:
return False
days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
weeks = ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"]
flag = isYear(year)
count = 0
for i in range(month-1):
count += days[i]
if not flag and month > 2:
count -= 1
count += int(day)
for i in range(1971, year):
if isYear(i):
count += 366
else:
count += 365
return weeks[(count-1)%7]
留言