[LeetCode] Number of Days in a Month

1118. Number of Days in a Month

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int numberOfDays(int year, int month) {
if(month == 2) {
if(year % 4) return 28;
if(year % 100) return 29;
return year % 400 == 0 ? 29 : 28;
}
if(month <= 7) {
return month & 1 ? 31 : 30;
}
return month & 1 ? 30 : 31;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/05/16/PS/LeetCode/number-of-days-in-a-month/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.