题目如下:

Given a string date representing a Gregorian calendar date formatted as YYYY-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: 41

Example 3:

Input: date = "2003-03-01"
Output: 60

Example 4:

Input: date = "2004-03-01"
Output: 61

Constraints:

  • date.length == 10
  • date[4] == date[7] == '-', and all other date[i]'s are digits
  • date 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])

最新文章

  1. 在SQL中 给字符串补0方法
  2. C#中委托实现的异步编程
  3. UVA 11987 Almost Union-Find (并查集+删边)
  4. python+redis测试环境搭建
  5. 文件上传利器SWFUpload入门简易教程
  6. URLEncode转json
  7. Supervisor的一些基础使用
  8. 你真的会玩SQL吗?内连接、外连接
  9. SpringMVC配置双数据源,一个java项目同时连接两个数据库
  10. Django rest framework:__str__ returned non-string (type NoneType) 真正原因
  11. PHP之工厂方法模式(三)
  12. 海量数据挖掘MMDS week4: 推荐系统之隐语义模型latent semantic analysis
  13. Node.js(day2)
  14. 开源后的.Net 如何选择使用
  15. JS笔记(三):数组、函数、类
  16. [Socket]Socket文件传输
  17. Nginx实战之让用户通过用户名密码认证访问web站点
  18. Paget Object 设计模式编写selenium测试用例
  19. Spring.NET依赖注入框架学习--入门
  20. POJ 1308 Is It A Tree?和HDU 1272 小希的迷宫

热门文章

  1. Django开启https(不用nginx)
  2. 学用 TStringGrid [6] - Options
  3. C# 文件打开对话框 图片fitter
  4. flask及扩展源码解读
  5. idea的掌握
  6. mysql中关于 like ,not like 的用法时不能显示空值的数据(空值不参与判断,直接过滤空值)
  7. python 并发编程 多进程 守护进程
  8. Mybatis—动态sql拼接问题
  9. 《剑指offer》面试题26 复杂链表的复制 Java版
  10. 2019.07.06 纪中_B