Skip to content

Latest commit

 

History

History

1118

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Given a year year and a month month, return the number of days of that month.

 

Example 1:

Input: year = 1992, month = 7
Output: 31

Example 2:

Input: year = 2000, month = 2
Output: 29

Example 3:

Input: year = 1900, month = 2
Output: 28

 

Constraints:

  • 1583 <= year <= 2100
  • 1 <= month <= 12

Companies:
Amazon

Related Topics:
Math

Solution 1.

// OJ: https://leetcode.com/problems/number-of-days-in-a-month/
// Author: github.com/lzl124631x
// Time: O(1)
// Space: O(1)
class Solution {
    bool isLeapYear(int year) {
        return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    }
public:
    int numberOfDays(int year, int month) {
        if (month == 2) return isLeapYear(year) ? 29 : 28;
        return month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ? 31 : 30;
    }
};