1118. Number of Days in a Month 一月有多少天
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:https://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/day-of-the-year/
Given a year Y and a month M, return how many days there are in that month.
Example 1:
Input: Y = 1992, M = 7Output: 31Example 2:
Input: Y = 2000, M = 2Output: 29Example 3:
Input: Y = 1900, M = 2Output: 28Note:
1583 <= Y <= 21001 <= M <= 12
判断给出的某年某月有多少天。
判断是否是闰年
Section titled “判断是否是闰年”这个题和1185. Day of the Week类似。
这个题中计算出当年是不是闰年,然后从数组中读取当月的天数。
C++代码如下:
class Solution {public: int numberOfDays(int Y, int M) { return daysOfMonth[isLeapYear(Y)][M - 1]; } bool isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } int daysOfMonth[2][12] = { {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} };};2019 年 9 月 18 日 —— 今日又是九一八

评论与交流