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];
    }
};
相关推荐
郝学胜-神的一滴7 小时前
算法实战:最小k个数——大顶堆的优雅解法
开发语言·数据结构·c++·python·程序人生·算法·排序算法
Irissgwe7 小时前
算法滑动窗口
数据结构·算法
bksczm7 小时前
linux之线程概念和控制
linux·开发语言·c++
怪兽学LLM7 小时前
LeetCode 105. 从前序与中序遍历序列构造二叉树:分治递归思路详解
算法·leetcode·职场和发展
可编程芯片开发7 小时前
基于PI控制的三相整流器控制系统的simulink建模与仿真,包含超级电容充电和电机
算法
hehelm8 小时前
Linux网络编程—TCP字典翻译系统
linux·开发语言·网络·c++·tcp/ip
酿情师8 小时前
区块链共识算法深度拆解:PoW、PoS、PBFT、Raft 原理解析
算法·区块链·共识算法
hansang_IR8 小时前
【记录】「SCOI2016」三道模拟赛/26.7.12
c++·算法
退休倒计时8 小时前
【每日一题】LeetCode 39. 组合总和 TypeScript
算法·leetcode·typescript
不是az8 小时前
力控相关知识点
算法·机器学习·最小二乘法