文章目录
- 1. 题目
- 2. 解题
1. 题目
指定年份 Y 和月份 M,请你帮忙计算出该月一共有多少天。
示例 1:
输入:Y = 1992, M = 7
输出:31示例 2:
输入:Y = 2000, M = 2
输出:29示例 3:
输入:Y = 1900, M = 2
输出:28提示:
1583 <= Y <= 2100
1 <= M <= 12
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-days-in-a-month
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题
class Solution {//C++
public:int numberOfDays(int Y, int M) {vector<int> d = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if(M != 2)return d[M];if((Y%400 == 0) || (Y%4==0 && Y%100!=0))return d[2]+1;return d[2];}
};
0 ms 6.1 MB
class Solution:#py3def numberOfDays(self, Y: int, M: int) -> int:d = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]if M != 2:return d[M]if (Y%400 == 0) or (Y%4==0 and Y%100!=0):return d[2]+1return d[2]
44 ms 13.5 MB
长按或扫码关注我的公众号,一起加油、一起学习进步!