志,敢教日月换新天。为有牺牲多壮

[Swift]LeetCode1185. 一周中的第几天 | Day of the Week

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(www.zengqiang.org
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11484998.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given a date, return the corresponding day of the week for that date.

The input is given as three integers representing the daymonth 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.

给你一个日期,请你设计一个算法来判断它是对应一周中的哪一天。

输入为三个整数:daymonth 和 year,分别表示日、月、年。

您返回的结果必须是这几个值中的一个 {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}

 

示例 1:

输入:day = 31, month = 8, year = 2019
输出:"Saturday"

示例 2:

输入:day = 18, month = 7, year = 1999
输出:"Sunday"

示例 3:

输入:day = 15, month = 8, year = 1993
输出:"Sunday"

 

提示:

  • 给出的日期一定是在 1971 到 2100 年之间的有效日期。

Runtime: 0 ms
Memory Usage: 20.5 MB
复制代码
 1 class Solution {
 2     let S:[String] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
 3     func dayOfTheWeek(_ day: Int, _ month: Int, _ year: Int) -> String {
 4         var month = month
 5         var year = year
 6         if month < 3
 7         {
 8             year -= 1
 9             month += 12
10         }
11         let w:Int = (year + year / 4 - year / 100 + year / 400 + (13 * month + 8) / 5 + day) % 7
12         return S[w]
13     }
14 }
复制代码

0ms

 

复制代码
 1 class Solution {
 2     func dayOfTheWeek(_ day: Int, _ month: Int, _ year: Int) -> String {
 3         let months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
 4         let days = ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"]
 5         var res = 0
 6         for y in stride(from: 1972, to: year, by: 4) {
 7             res += 1
 8         }
 9         if year % 4 == 0 && year != 2100 && month > 2 {
10             res += 1
11         }
12         res += (year - 1971) * 365
13         for m in 1..<month {
14             res += months[m - 1]
15         }
16         res += (day - 1)
17         res %= 7
18         return days[res]
19     }
20 }
复制代码

4ms

复制代码
 1 class Solution {
 2     func dayOfTheWeek(_ day: Int, _ month: Int, _ year: Int) -> String {
 3         guard day >= 1 && day <= 31 && month >= 1 && month <= 12 && year >= 1971 && year <= 2100 else {
 4             return ""
 5         }
 6         
 7         func isLeapYear(_ year: Int) -> Bool {
 8             return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
 9         }
10         
11         // 1971-1-1 => Friday
12         let weekName = ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"]
13         // Days to 01-01, leadYear
14         let monthDays = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]
15         var days = 0
16      
17         for y in 1971 ..< year {
18             days += (isLeapYear(y) ? 366 : 365)
19         }
20         
21         if month < 3 {
22             days += monthDays[month - 1] + day - 1
23         }
24         else {
25             days += (monthDays[month - 1] + (isLeapYear(year) ? 0 : -1) + day - 1)
26         }
27         
28         return weekName[days % 7]
29     }
30 }
复制代码

8ms

复制代码
 1 class Solution {
 2     func dayOfTheWeek(_ day: Int, _ month: Int, _ year: Int) -> String {
 3         let formatter  = DateFormatter()
 4         formatter.dateFormat = "yyyy-MM-dd"
 5         let date = formatter.date(from: "\(year)-\(month)-\(day)")
 6         let myCalendar = Calendar(identifier: .gregorian)
 7         let weekDay = myCalendar.component(.weekday, from: date!)
 8         
 9         return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][weekDay - 1]
10     }
11 }
复制代码

 

posted @   为敢技术  阅读(515)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°