LeetCode每日一题——1185. Day of the Week

文章目录

一、题目

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.

二、题解

使用基姆拉尔森公式计算

cpp 复制代码
class Solution {
public:
    string dayOfTheWeek(int day, int month, int year) {
        vector<string> res = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        if(month == 1 || month == 2) month += 12,year--;
        int week = (day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+1)%7;
        return res[week];
    }
};
相关推荐
一座城和一个人21 分钟前
2026年复旦大学计算机考研复试机试真题解析
数据结构·考研·算法·考研机试·复试上机
Nuanyt22 分钟前
LeetCode Hot 100 贪心算法专题 121 / 55 / 45 / 763 (Java)
java·算法·leetcode·贪心算法·dp
wuminyu30 分钟前
OpenJDK学习笔记:如何构建支持动态注册、注销和重新注册的 Native回调管理器
java·linux·c语言·jvm·c++
Escalating_xu35 分钟前
【Redis C++】Hash、ZSet 与集群:从对象字段到积分排行榜
c++·redis·哈希算法
AI的探索之旅1 小时前
OpenCV Day48 —— SVM 手写数字:HOG 与 Hellinger 核
算法·机器学习·支持向量机
金榜题名萱1 小时前
蓝桥杯 - 拍照 —— 动态规划
c语言·数据结构·c++·算法·职场和发展·蓝桥杯·动态规划
fthux1 小时前
图灵测试:一场跨越七十余年的智能之问
人工智能·算法
Shaoshing1 小时前
LeetCode100 560.和为k的子数组
java·数据结构·算法
闻缺陷则喜何志丹3 小时前
【C++20特性】std::format
c++·字符串·c++20·格式化
专注仿真9 小时前
仿真智能体训练(评估篇):基于蒙特卡洛批量对抗的评估算法详解与实战
算法·仿真·评估·afsim智能体·仿真训练