-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1154-DayOfTheYear.cs
41 lines (34 loc) · 1.06 KB
/
1154-DayOfTheYear.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//-----------------------------------------------------------------------------
// Runtime: 80ms
// Memory Usage: 22.6 MB
// Link: https://leetcode.com/submissions/detail/351839198/
//-----------------------------------------------------------------------------
using System.Linq;
namespace LeetCode
{
public class _1154_DayOfTheYear
{
public int DayOfYear(string date)
{
var days = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
var split = date.Split('-').Select(s => int.Parse(s)).ToArray();
var year = split[0];
var count = 0;
for (int i = 0; i < 12; i++)
{
if (split[1] == i + 1)
{
count += split[2];
break;
}
else
{
count += days[i];
}
}
if (split[1] > 2)
if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0)) count++;
return count;
}
}
}