Leetcode 1154. 一年中的第几天
1.题目基本信息
1.1.题目描述
给你一个字符串 date ,按 YYYY-MM-DD 格式表示一个 现行公元纪年法 日期。返回该日期是当年的第几天。
1.2.题目地址
https://leetcode.cn/problems/day-of-the-year/description/
2.解题方法
2.1.解题思路
直接计算
知识点:闰年判定+月份天数
3.解题代码
python代码
class Solution:
def dayOfYear(self, date: str) -> int:
# 思路:直接计算。注意:闰年判定+月份天数判定
days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
year, month, day = date.split("-")
year, month, day = int(year), int(month), int(day)
result = sum(days[:month]) + day
# 闰年情况
if month > 2 and (year % 400 == 0 or (year % 100 != 0 and year % 4 == 0)):
result += 1
return result