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];
    }
};
相关推荐
新时代苦力工4 分钟前
桶排序-Java实现
数据结构·算法·排序算法
zh_xuan15 分钟前
duiLib 自定义资源目录
c++·ui
西红柿煎蛋33 分钟前
C++11的可变参数模板 (Variadic Templates) 是如何工作的?如何使用递归解包一个参数包 (parameter pack)?
c++
源代码•宸1 小时前
深入浅出设计模式——创建型模式之原型模式 Prototype
c++·经验分享·设计模式·原型模式
爱吃芒果的蘑菇1 小时前
Python读取获取波形图波谷/波峰
python·算法
晨曦学习日记1 小时前
Leetcode239:滑动窗口最大值,双端队列的实现!
数据结构·c++·算法
CoovallyAIHub1 小时前
无人机图像+深度学习:湖南农大团队实现稻瘟病分级检测84%准确率
深度学习·算法·计算机视觉
2zcode1 小时前
基于Matlab自适应阈值分割算法的图像处理研究
图像处理·算法·matlab
阿群今天学习了吗2 小时前
RNN、LSTM、Transformer推荐博文
人工智能·笔记·python·学习·算法
wait a minutes2 小时前
【c++】leetcode763 划分字母区间
开发语言·c++