1088. Number of Days in a Month

  • Difficulty: Easy

  • Topics:

  • Similar Questions:

Problem:

Given a year Y and a month M, return how many days there are in that month.

 

Example 1:

Input: Y = 1992, M = 7
Output: 31

Example 2:

Input: Y = 2000, M = 2
Output: 29

Example 3:

Input: Y = 1900, M = 2
Output: 28

 

Note:

  1. 1583 <= Y <= 2100
  2. 1 <= M <= 12

Solutions:

class Solution {
public:
    int numberOfDays(int Y, int M) {
        int dayNum[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (Y % 100 == 0) {
            if (Y % 400 == 0) {
                dayNum[1] = 29;
            }
        } else {
            if (Y % 4 == 0) {
                dayNum[1] = 29;
            }
        }

        return dayNum[M - 1];
    }
};

results matching ""

    No results matching ""