Skip to content

Commit 748da57

Browse files
authored
Update day-of-the-year.py
1 parent a272598 commit 748da57

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Python/day-of-the-year.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22
# Space: O(1)
33

44
class Solution(object):
5+
def __init__(self):
6+
def dayOfMonth(M):
7+
return (28 if (M == 2) else 31-(M-1)%7%2)
8+
9+
self.__lookup = [0]*12
10+
for M in xrange(1, len(self.__lookup)):
11+
self.__lookup[M] += self.__lookup[M-1]+dayOfMonth(M)
12+
13+
def dayOfYear(self, date):
14+
"""
15+
:type date: str
16+
:rtype: int
17+
"""
18+
Y, M, D = map(int, date.split("-"))
19+
leap = 1 if M > 2 and (((Y % 4 == 0) and (Y % 100 != 0)) or (Y % 400 == 0)) else 0
20+
return self.__lookup[M-1]+D+leap
21+
22+
23+
# Time: O(1)
24+
# Space: O(1)
25+
class Solution2(object):
526
def dayOfYear(self, date):
627
"""
728
:type date: str

0 commit comments

Comments
 (0)