Day of the Year LeetCode solution in Python

1
class Solution:
2
def dayOfYear(self, date: str) -> int:
3
d = {
4
1: 31,
5
2: 28,
6
3: 31,
7
4: 30,
8
5: 31,
9
6: 30,
10
7: 31,
11
8: 31,
12
9: 30,
13
10: 31,
14
11: 30,
15
12: 31,
16
}
17
year = int(date[:4])
18
if year % 4 == 0:
19
if year % 100 == 0:
20
if year % 400 == 0:
21
d[2] = 29
22
else:
23
d[2] = 29
24
month = int(date[5:7])
25
day = int(date[8:])
26
ans = 0
27
for i in range(1, month + 1):
28
ans += d[i]
29
return ans - (d[month] - day)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0