[
math
]
Leetcode 1118 Number of Days in a Month
Problem statement
https://leetcode.com/problems/number-of-days-in-a-month/
Solution
The only difficulty we have is how to deal with february: for this we check conditions: if it is divisible by 400, then 100, then 4.
Complexity
Time complexity is O(1)
, space is O(1)
as well.
Code
class Solution:
def numberOfDays(self, year, month):
arr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if year % 400 == 0:
gap = 1
elif year % 100 == 0:
gap = 0
elif year % 4 == 0:
gap = 1
else:
gap = 0
return arr[month - 1] + gap * (month == 2)