1118. Number of Days in a Month ¶ Time: $O(1)$ Space: $O(1)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10 11 12 13class Solution { public: int numberOfDays(int year, int month) { const vector<int> days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; return month == 2 && isLeapYear(year) ? 29 : days[month]; } private: bool isLeapYear(int year) { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; }; }; 1 2 3 4 5 6 7 8 9 10class Solution { public int numberOfDays(int year, int month) { final int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; return month == 2 && isLeapYear(year) ? 29 : days[month]; } private boolean isLeapYear(int year) { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } } 1 2 3 4 5 6 7class Solution: def numberOfDays(self, year: int, month: int) -> int: days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] def isLeapYear(year: int) -> bool: return year % 4 == 0 and year % 100 != 0 or year % 400 == 0 return 29 if month == 2 and isLeapYear(year) else days[month]