方法一 列举法:
用一个数组把每个月份的天数都列举出来
判断闰年,是闰年2月份有29天
循环对当前月份之前的月份天数求和
加上当天月份的天数
var dayOfYear = function(date) {
let year = +date.slice(0, 4);
let month = +date.slice(5, 7);
let day = +date.slice(8);
let amount = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if (year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) {
++amount[1];
}
let ans = 0;
for (let i = 0; i < month - 1; ++i) {
ans += amount[i];
}
return ans + day;
};
消耗时间和内存情况:文章来源:https://www.toymoban.com/news/detail-784179.html
文章来源地址https://www.toymoban.com/news/detail-784179.html
到了这里,关于每日一题——LeetCode1154.一年中的第几天的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!