首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Leetcode 1154. Day of the Year

Leetcode 1154. Day of the Year

作者头像
Tyan
发布于 2021-08-13 11:56:34
发布于 2021-08-13 11:56:34
5140
举报
文章被收录于专栏:SnailTyanSnailTyan

1. Description

2. Solution

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

  • Version 1
代码语言:javascript
复制
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/
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/08/11 ,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 1. Description
  • 2. Solution
  • Reference
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档