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

[Swift]LeetCode1154. 一年中的第几天 | Ordinal Number Of Date

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

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

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

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

Given a string date representing a (Gregorian) calendar date formatted as YYYY-MM-DD, return the ordinal number of that day in the same year.

(For example, Jan 1st has ordinal number 1, Jan 2nd has ordinal number 2, and so on.  For more information on the number of days in each month, see [Wiki: Gregorian Calendar]) 

Example 1:

Input: date = "2019-01-09"
Output: 9

Example 2:

Input: date = "2019-02-10"
Output: 41

Example 3:

Input: date = "2003-03-01"
Output: 60

Example 4:

Input: date = "2004-03-01"
Output: 61 

Constraints:

  • date.length == 10
  • date[4] == date[7] == '-', and all other date[i]'s are digits
  • date represents a calendar date between Jan 1st, 1900 and Dec 31, 2019.

给你一个按 YYYY-MM-DD 格式表示日期的字符串 date,请你计算并返回该日期是当年的第几天。

通常情况下,我们认为 1 月 1 日是每年的第 1 天,1 月 2 日是每年的第 2 天,依此类推。每个月的天数与现行公元纪年法(格里高利历)一致。 

示例 1:

输入:date = "2019-01-09"
输出:9

示例 2:

输入:date = "2019-02-10"
输出:41

示例 3:

输入:date = "2003-03-01"
输出:60

示例 4:

输入:date = "2004-03-01"
输出:61 

提示:

  • date.length == 10
  • date[4] == date[7] == '-',其他的 date[i] 都是数字。
  • date 表示的范围从 1900 年 1 月 1 日至 2019 年 12 月 31 日。

Runtime: 4 ms
Memory Usage: 21.3 MB
 1 class Solution {
 2     func dayOfYear(_ date: String) -> Int {
 3         let arrDate:[Int] = Array(date).map{$0.ascii}
 4         var Y:Int = 0
 5         var M:Int = 0
 6         var D:Int = 0
 7         Y+=(arrDate[0] - 48)*1000
 8         Y+=(arrDate[1] - 48)*100
 9         Y+=(arrDate[2] - 48)*10
10         Y+=(arrDate[3] - 48)*1
11         M+=(arrDate[5] - 48)*10
12         M+=(arrDate[6] - 48)*1
13         D+=(arrDate[8] - 48)*10
14         D+=(arrDate[9] - 48)*1
15         M -= 1
16         var Ds:[Int] = [31,28,31,30,31,30,31,31,30,31,30,31]
17         if Y%400 == 0
18         {
19             Ds[1] += 1
20         }
21         else if Y % 100 != 0 && Y % 4 == 0
22         {
23             Ds[1] += 1
24         }
25         for i in 0..<M
26         {
27             D+=Ds[i]
28         }
29         return D        
30     }
31 }
32 
33 //Character扩展 
34 extension Character  
35 {  
36   //Character转ASCII整数值(定义小写为整数值)
37    var ascii: Int {
38        get {
39            return Int(self.unicodeScalars.first?.value ?? 0)
40        }       
41     }
42 }

4ms

 

 1 class Solution {
 2     func dayOfYear(_ date: String) -> Int {
 3         let days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
 4         let arr = Array(date)
 5         let y = Int(String(arr[0..<4]))!
 6         let m = Int(String(arr[5..<7]))!
 7         let d = Int(String(arr[8...]))!
 8         var res = 0
 9         for i in 0..<(m - 1) {
10             res += days[i]
11         }
12         res += d
13         if m > 2 && isLeap(y) {
14             res += 1
15         }
16         return res
17     }
18     
19     func isLeap(_ y: Int) -> Bool {
20         if y % 4 != 0 { return false }
21         else if y % 100 != 0 { return true }
22         else if y % 400 != 0 { return false }
23         return true
24     }
25 }

8ms

 1 class Solution {    
 2     func dayOfYear(_ date: String) -> Int {
 3         let dateParts = date.split(separator: "-")
 4         if dateParts.count != 3 {
 5             return -1
 6         }
 7         else {
 8             guard let year = Int(dateParts[0]) else {
 9                 return -1
10             }
11             guard let month = Int(dateParts[1]) else {
12                 return -1
13             }
14             guard let day = Int(dateParts[2]) else {
15                 return -1
16             }
17             
18             var isLeapYear = (year % 4 == 0)
19             if (year % 100 == 0) {
20                 isLeapYear = (year % 400 == 0)
21             }
22             let monthToCumulativeNumDays = getMonthToCumulativeNumDays(isLeapYear)
23             guard let cumulative = monthToCumulativeNumDays[month-1] else {
24                 return -1
25             }
26             return cumulative + day
27         }
28     }
29     
30     func getMonthToCumulativeNumDays(_ leapYear : Bool) -> [Int: Int]{
31         var monthToCumulativeNumDays: [Int: Int] = [:]
32         monthToCumulativeNumDays[0] = 0
33         monthToCumulativeNumDays[1] = 31
34         let febDays = leapYear ? 29 : 28
35         monthToCumulativeNumDays[2] = febDays + monthToCumulativeNumDays[1]!
36         monthToCumulativeNumDays[3] = 31 + monthToCumulativeNumDays[2]!
37         monthToCumulativeNumDays[4] = 30 + monthToCumulativeNumDays[3]!
38         monthToCumulativeNumDays[5] = 31 + monthToCumulativeNumDays[4]!
39         monthToCumulativeNumDays[6] = 30 + monthToCumulativeNumDays[5]!
40         monthToCumulativeNumDays[7] = 31 + monthToCumulativeNumDays[6]!
41         monthToCumulativeNumDays[8] = 31 + monthToCumulativeNumDays[7]!
42         monthToCumulativeNumDays[9] = 30 + monthToCumulativeNumDays[8]!
43         monthToCumulativeNumDays[10] = 31 + monthToCumulativeNumDays[9]!
44         monthToCumulativeNumDays[11] = 30 + monthToCumulativeNumDays[10]!
45         monthToCumulativeNumDays[12] = 31 + monthToCumulativeNumDays[11]!
46         return monthToCumulativeNumDays
47     }
48 }

12ms

 1 class Solution {
 2     struct Components {
 3         let day : Int
 4         let month : Int
 5         let year : Int
 6         
 7         init(withString s: String) {
 8             let comps = s.components(separatedBy:"-")
 9             year = Int(comps[0]) ?? 0
10             month = Int(comps[1]) ?? 0
11             day = Int(comps[2]) ?? 0
12         }
13         
14         func isLeap() -> Bool {
15             guard year % 4 == 0 else {
16                 return false
17             }
18             return year % 100 != 0 || year % 400 == 0
19         }
20         func daysInMonth(_ m: Int) -> Int {
21             let dayCache = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
22             if m == 2 && isLeap() {
23                 return 29
24             }
25             return dayCache[m-1]
26         }
27     }
28     func dayOfYear(_ date: String) -> Int {
29         let c = Components(withString: date)
30         var result = 0
31         var month = 1
32         while month < c.month {
33             result += c.daysInMonth(month)
34             month += 1
35         }
36         return result + c.day
37     }
38 }

36ms

 1 class Solution {
 2     func dayOfYear(_ date: String) -> Int {
 3         let year = date[..<date.index(date.startIndex, offsetBy: 4)]
 4         let begin = "\(year)-01-01"
 5 
 6         let format = DateFormatter()
 7         format.dateFormat = "yyyy-MM-dd"
 8 
 9         let startDate = format.date(from: begin)!
10         let endDate = format.date(from: date)!
11 
12         let calendar = Calendar(identifier: .gregorian)
13         let components = calendar.dateComponents([.day], from: startDate, to: endDate)
14         return components.day!+1
15     }
16 }

40ms

 1 class Solution {
 2     func dayOfYear(_ date: String) -> Int {
 3         let dateFormatter = DateFormatter()
 4         dateFormatter.dateFormat = "yyyy-MM-dd"
 5         let endDate = dateFormatter.date(from: date)!
 6         let year = date.components(separatedBy: "-").first!
 7         let startStr = year + "-01-01"
 8         let startDate = dateFormatter.date(from: startStr)!
 9         
10         let days = Calendar.current.dateComponents([.day], from: startDate, to: endDate).day!
11         return days + 1
12     }
13 }

 

posted @ 2019-08-11 08:51  为敢技术  阅读(521)  评论(0编辑  收藏  举报