#1118
Easy Algorithms Number of days in a month
Math
59.4% acceptance
Mar 31, 2026
47
180
Given a year year and a month month, return the number of days of that month.
Solution
Rust
Time O(1)
Space O(1)
impl Solution {
pub fn number_of_days(year: i32, month: i32) -> i32 {
let days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if month == 2 && (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
29
} else {
days[(month - 1) as usize]
}
}
}