Leetcode 1154. Day of the Year

Posted SnailTyan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 1154. Day of the Year相关的知识,希望对你有一定的参考价值。

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

2. Solution

**解析:**Version 1,先将年、月、日分离出来,然后根据月份和日期累加天数,最后判断时闰年的话总天数加1

  • Version 1
class Solution:
    def dayOfYear(self, date: str) -> int:
        year, month, day = map(int, date.split('-'))
        months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        days = day
        for i in range(month):
            days += months[i]
        if month > 2 and (year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)):
            days += 1
        return days

Reference

  1. https://leetcode.com/problems/day-of-the-year/

以上是关于Leetcode 1154. Day of the Year的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode --- 1185. Day of the Week 解题报告

LeetCode --- 1185. Day of the Week 解题报告

Leetcode-1154 Ordinal Number Of Date(一年中的第几天)

6days before the day of your missed period(5 days before the day of your expected period 啥意思

the Way of Python Day 2

the Way of Python Day 3