【leetcode】1154. Day of the Year
2024-10-07 09:07:29
题目如下:
Given a string
date
representing a Gregorian calendar date formatted asYYYY-MM-DD
, return the day number of the year.Example 1:
Input: date = "2019-01-09"
Output: 9
Explanation: Given date is the 9th day of the year in 2019.Example 2:
Input: date = "2019-02-10"
Output: 41Example 3:
Input: date = "2003-03-01"
Output: 60Example 4:
Input: date = "2004-03-01"
Output: 61Constraints:
date.length == 10
date[4] == date[7] == '-'
, and all otherdate[i]
's are digitsdate
represents a calendar date between Jan 1st, 1900 and Dec 31, 2019.
解题思路:题目很简单,注意区分闰年和平年即可。
代码如下:
class Solution(object):
def dayOfYear(self, date):
"""
:type date: str
:rtype: int
"""
date = date.split('-')
year = date[0]
def isLeapYear(year):
return (year % 4) == 0 and (year % 100) != 0 or (year % 400) == 0
month_list = [31,28,31,30,31,30,31,31,30,31,30,31]
if isLeapYear(int(year)):
month_list[1] += 1
month = int(date[1])
return sum(month_list[:month-1]) + int(date[2])
最新文章
- 在SQL中 给字符串补0方法
- C#中委托实现的异步编程
- UVA 11987 Almost Union-Find (并查集+删边)
- python+redis测试环境搭建
- 文件上传利器SWFUpload入门简易教程
- URLEncode转json
- Supervisor的一些基础使用
- 你真的会玩SQL吗?内连接、外连接
- SpringMVC配置双数据源,一个java项目同时连接两个数据库
- Django rest framework:__str__ returned non-string (type NoneType) 真正原因
- PHP之工厂方法模式(三)
- 海量数据挖掘MMDS week4: 推荐系统之隐语义模型latent semantic analysis
- Node.js(day2)
- 开源后的.Net 如何选择使用
- JS笔记(三):数组、函数、类
- [Socket]Socket文件传输
- Nginx实战之让用户通过用户名密码认证访问web站点
- Paget Object 设计模式编写selenium测试用例
- Spring.NET依赖注入框架学习--入门
- POJ 1308 Is It A Tree?和HDU 1272 	小希的迷宫