LeetCode 1154. 一年中的第几天
原题链接
简单
作者:
我是java同学
,
2023-12-31 07:02:23
,
所有人可见
,
阅读 35
class Solution {
public:
int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool is_leap(int year) {
if (year % 400 == 0 || year % 100 && year % 4 == 0)
return true;
return false;
}
int get_days(int year, int month) {
int res = months[month];
if (month == 2) res += is_leap(year);
return res;
}
int dayOfYear(string date) {
int res = 0;
int year = stoi(date.substr(0, 4));
int month = stoi(date.substr(5, 2));
int day = stoi(date.substr(8));
for (int i = 1; i < month; i ++ )
res += get_days(year, i);
res += day;
return res;
}
};