1118. Number of Days in a Month

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

Rust Solution

struct Solution;

impl Solution {
    fn number_of_days(y: i32, m: i32) -> i32 {
        let days = vec![31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        if m == 2 {
            if y % 4 == 0 && y % 100 != 0 || y % 400 == 0 {
                1 + days[1]
            } else {
                days[1]
            }
        } else {
            days[m as usize - 1]
        }
    }
}

#[test]
fn test() {
    let y = 1992;
    let m = 7;
    let res = 31;
    assert_eq!(Solution::number_of_days(y, m), res);
    let y = 2000;
    let m = 2;
    let res = 29;
    assert_eq!(Solution::number_of_days(y, m), res);
    let y = 1900;
    let m = 2;
    let res = 28;
    assert_eq!(Solution::number_of_days(y, m), res);
}

Having problems with this solution? Click here to submit an issue on github.