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];
    }
};
相关推荐
北风toto18 小时前
中缀、前缀、后缀表达式
算法·软件设计师
听取WA声一片(无恶意)18 小时前
CSP-J/CSP-S 深度优先搜索(DFS)完全讲义
c++·算法·深度优先
码匠许师傅19 小时前
【C++ 面试真题】27. 聊聊 C++ 的内存泄漏与内存布局
java·c++·面试
码流怪侠19 小时前
2026年8月GitHub热榜深度拆解:Agent Skills席卷开源圈,一个“技能包“收割5万星
算法·程序员·github
hold?fish:palm19 小时前
30 两两交换链表中的节点
数据结构·算法·链表
Nil20819 小时前
leetcode 98验证二叉搜索树
算法·leetcode·职场和发展
kkkkkkkkkk_Z19 小时前
学嵌入式和C语言编程数据结构|学习日记Day21:内核链表与队列学习
c语言·数据结构·学习
不正经学生20 小时前
C语言结构体:自定义数据类型,让变量打包出行
java·c语言·开发语言·数据结构·算法
加油码20 小时前
动静态库的链接底层原理(进阶版)
linux·c++
一木 之林20 小时前
三、内存管理、指针与堆栈
c语言·jvm·c++