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];
    }
};
相关推荐
范纹杉想快点毕业20 分钟前
C++多重继承详解与实战解析
开发语言·c++
太空1号2 小时前
飞腾D2000,麒麟系统V10,docker,ubuntu1804,小白入门喂饭级教程
linux·c++·docker
Matlab仿真实验室2 小时前
基于Matlab实现LDA算法
开发语言·算法·matlab
@蓝莓果粒茶2 小时前
LeetCode第244题_最短单词距离II
c++·笔记·学习·算法·leetcode·职场和发展·c#
iCxhust2 小时前
Deepseek给出的8255显示例程
c语言·开发语言·c++·单片机·嵌入式硬件
宋一诺333 小时前
机器学习——随机森林算法
算法·随机森林·机器学习
无聊的小坏坏3 小时前
二分查找的边界艺术:LeetCode 34 题深度解析
算法·leetcode
CHNLee玉米3 小时前
题目解析 1.找单独的数 | 豆包MarsCode AI刷题
算法
緈福的街口3 小时前
【leetcode】20. 有效的括号
linux·算法·leetcode
PixelMind4 小时前
【LUT技术专题】图像自适应3DLUT代码讲解
人工智能·python·算法·lut