博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode题解(1154):判断日期在一年中的第几天(Python)
阅读量:1899 次
发布时间:2019-04-26

本文共 990 字,大约阅读时间需要 3 分钟。

题目:(简单)

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( 1 ) O(1) O(1) O ( 1 ) O(1) O(1) 32ms (96.43%)
Ans 2 (Python) O ( 1 ) O(1) O(1) O ( 1 ) O(1) O(1) 40ms (72.62%)
Ans 3 (Python)

LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。

解法一:

def dayOfYear(self, date: str) -> int:    year, month, date = date.split("-")    year, month, date = int(year), int(month), int(date)    leap = year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)    if leap:        month_days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]    else:        month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]    return sum(month_days[:month - 1]) + date

解法二(优雅化):

def dayOfYear(self, date: str) -> int:    year, month, date = map(int, date.split("-"))    if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):        month_days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]    else:        month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]    return sum(month_days[:month - 1]) + date

转载地址:http://uqgdf.baihongyu.com/

你可能感兴趣的文章
linux交叉编译的库在链接时提示:xxx uses VFP register arguments xxx does not
查看>>
利用__attribute__((section()))构建初始化函数表与Linux内核init的实现
查看>>
melis cedar模块的链接脚本
查看>>
RTThread IO设备和驱动学习
查看>>
编译报错: error: expected ‘=‘, ‘,‘, ‘;‘, ‘asm‘ or ‘__attribute__‘ before *‘ token
查看>>
mmap函数实现
查看>>
音频基础知识详解
查看>>
Linux 命令xxd功能
查看>>
repo使用指南
查看>>
gstreamer-test
查看>>
GDB基本用法
查看>>
动态范围控制(DRC)简介
查看>>
使用C语言查看一个文件夹中所有文件及目录
查看>>
音频硬件基础
查看>>
TS流分析
查看>>
详解YUV420数据格式
查看>>
Gstreamer学习笔记(2):GstElement定义、连接
查看>>
GStreamer建议的学习步骤和网页链接汇总
查看>>
Ubuntu14.04编译安装GStreamer
查看>>
GStreamer(一)
查看>>