1185. 一周中的第几天
给你一个日期,请你设计一个算法来判断它是对应一周中的哪一天。
输入为三个整数:day
、month
和 year
,分别表示日、月、年。
您返回的结果必须是这几个值中的一个 {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
。
示例 1:
输入:day = 31, month = 8, year = 2019 输出:"Saturday"
示例 2:
输入:day = 18, month = 7, year = 1999 输出:"Sunday"
示例 3:
输入:day = 15, month = 8, year = 1993 输出:"Sunday"
提示:
1971
到 2100
年之间的有效日期。原站题解
python3 解法, 执行用时: 32 ms, 内存消耗: 17 MB, 提交时间: 2023-12-30 00:12:25
class Solution: def dayOfTheWeek(self, day: int, month: int, year: int) -> str: return datetime.datetime(year,month,day).strftime("%A")
cpp 解法, 执行用时: 0 ms, 内存消耗: 6.1 MB, 提交时间: 2023-12-30 00:11:05
class Solution { public: string dayOfTheWeek(int day, int month, int year) { if(month==1||month==2) month+=12,year--; int iWeek = (day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7; //基姆拉尔森计算公式 string result[]= { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday"}; return result[iWeek]; } };
javascript 解法, 执行用时: 56 ms, 内存消耗: 47.5 MB, 提交时间: 2023-12-30 00:09:08
/** * @param {number} day * @param {number} month * @param {number} year * @return {string} */ var dayOfTheWeek = function(day, month, year) { const week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; const monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]; /* 输入年份之前的年份的天数贡献 */ let days = 365 * (year - 1971) + Math.floor((year - 1969) / 4); /* 输入年份中,输入月份之前的月份的天数贡献 */ for (let i = 0; i < month - 1; ++i) { days += monthDays[i]; } if ((year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) && month >= 3) { days += 1; } /* 输入月份中的天数贡献 */ days += day; return week[(days + 3) % 7]; };
golang 解法, 执行用时: 0 ms, 内存消耗: 1.8 MB, 提交时间: 2023-12-30 00:08:28
var week = []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} var monthDays = []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30} func dayOfTheWeek(day, month, year int) string { days := 0 // 输入年份之前的年份的天数贡献 days += 365*(year-1971) + (year-1969)/4 // 输入年份中,输入月份之前的月份的天数贡献 for _, d := range monthDays[:month-1] { days += d } if month >= 3 && (year%400 == 0 || year%4 == 0 && year%100 != 0) { days++ } // 输入月份中的天数贡献 days += day return week[(days+3)%7] }
java 解法, 执行用时: 0 ms, 内存消耗: 39.9 MB, 提交时间: 2023-12-30 00:07:58
class Solution { public String dayOfTheWeek(int day, int month, int year) { String[] week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30}; /* 输入年份之前的年份的天数贡献 */ int days = 365 * (year - 1971) + (year - 1969) / 4; /* 输入年份中,输入月份之前的月份的天数贡献 */ for (int i = 0; i < month - 1; ++i) { days += monthDays[i]; } if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && month >= 3) { days += 1; } /* 输入月份中的天数贡献 */ days += day; return week[(days + 3) % 7]; } }
cpp 解法, 执行用时: 4 ms, 内存消耗: 6.2 MB, 提交时间: 2023-12-30 00:07:28
class Solution { public: string dayOfTheWeek(int day, int month, int year) { vector<string> week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; vector<int> monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30}; /* 输入年份之前的年份的天数贡献 */ int days = 365 * (year - 1971) + (year - 1969) / 4; /* 输入年份中,输入月份之前的月份的天数贡献 */ for (int i = 0; i < month - 1; ++i) { days += monthDays[i]; } if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && month >= 3) { days += 1; } /* 输入月份中的天数贡献 */ days += day; return week[(days + 3) % 7]; } };
python3 解法, 执行用时: 28 ms, 内存消耗: 17 MB, 提交时间: 2023-12-30 00:06:58
class Solution: def dayOfTheWeek(self, day: int, month: int, year: int) -> str: week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30] days = 0 # 输入年份之前的年份的天数贡献 days += 365 * (year - 1971) + (year - 1969) // 4 # 输入年份中,输入月份之前的月份的天数贡献 days += sum(monthDays[:month-1]) if (year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)) and month >= 3: days += 1 # 输入月份中的天数贡献 days += day return week[(days + 3) % 7]
golang 解法, 执行用时: 0 ms, 内存消耗: 2 MB, 提交时间: 2021-06-10 23:26:42
func dayOfTheWeek(day int, month int, year int) string { return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local).Weekday().String() }