LeetCode.1154-一年中的第几天(Day of the Year)
这是小川的第410次更新,第442篇原创
看题和准备
今天介绍的是LeetCode算法题中Easy级别的第261题(顺位题号是1154)。给定表示格式为YYYY-MM-DD的公历日期的字符串日期,返回该日期在年份中的编号。例如:
输入:date ="2019-01-09"
输出:9
说明:给定日期是2019年的第9天。
输入:date ="2019-02-10"
输出:41
输入:date ="2003-03-01"
输出:60
输入:date ="2004-03-01"
输出:61
注意:
date.length= 10date[4] == date[7] == '-',所有其他
date[i]都是数字日期表示1900年1月1日至2019年12月31日之间的日历日期。
第一种解法
题目的意思是计算给的日期在年份中的天数,年份分平年、闰年,月份分大月、小月,直接对日期字符串进行截取,转为对应的年月日数字。针对年份的处理,如果是闰年,则2月有29天,针对月份的处理,判断月份,然后累加对应的天数即可。
public int dayOfYear(String date) {
int year = Integer.parseInt(date.substring(0, 4));
int month = Integer.parseInt(date.substring(5, 7));
int day = Integer.parseInt(date.substring(8, 10));
switch (month) {
case 12:
day += 30;
case 11:
day += 31;
case 10:
day += 30;
case 9:
day += 31;
case 8:
day += 31;
case 7:
day += 30;
case 6:
day += 31;
case 5:
day += 30;
case 4:
day += 31;
case 3:
day += 28;
if (isLeapYear(year)) {
day++;
}
case 2:
day += 31;
}
return day;
}
/**
* 判断当前年份是否为闰年
* @param year
* @return
*/
public boolean isLeapYear(int year) {
// 普通闰年,能被4整除,但是后两位不以00结尾
if (year%4 == 0 && year%100 != 0) {
return true;
}
// 世纪闰年,后两位以00结尾,且能被400整除
if (year%100 == 0 && year%400 == 0) {
return true;
}
return false;
}
第二种解法
针对第一种解法中的switch语句,我们可以用数组进行替代,可以减少代码量。以月份为索引,天数为value,使用循环来处理天数,至于闰年的判断则不变。
public int dayOfYear2(String date) {
int[] arr = {31,28,31,30,31,30,31,31,30,31,30,31};
int year = Integer.parseInt(date.substring(0, 4));
int month = Integer.parseInt(date.substring(5, 7));
int day = Integer.parseInt(date.substring(8, 10));
for (int i=0; i<month-1; i++) {
day += arr[i];
}
return isLeapYear(year) && month >= 3 ? day+1 : day;
}
/**
* 判断当前年份是否为闰年
* @param year
* @return
*/
public boolean isLeapYear(int year) {
// 普通闰年,能被4整除,但是后两位不以00结尾
if (year%4 == 0 && year%100 != 0) {
return true;
}
// 世纪闰年,后两位以00结尾,且能被400整除
if (year%100 == 0 && year%400 == 0) {
return true;
}
return false;
}
第三种解法
既然我们已经知道了每一个月有多少天,那么我们可以直接将依次累计的天数放入数组中,月份减1为数组索引。
创建一个长度为13的数组,第一项为0,因为一月可以直接获取天数显示即可,第二项为31天,因为二月的天数需要加上一月的31天,依次往后计算,此数组中的月份累计天数以平年为基础。如果当前年份是闰年,并且月份大于2月,则需要在计算结果上多加一天。
public int dayOfYear3(String date) {
int[] arr = {0,31,59,90,120,151,181,212,243,273,304,334,365};
int year = Integer.parseInt(date.substring(0, 4));
int month = Integer.parseInt(date.substring(5, 7));
int day = Integer.parseInt(date.substring(8, 10));
if (isLeapYear(year) && month >= 3) {
return arr[month-1]+day+1;
}
return arr[month-1]+day;
}
/**
* 判断当前年份是否为闰年
* @param year
* @return
*/
public boolean isLeapYear(int year) {
// 普通闰年,能被4整除,但是后两位不以00结尾
if (year%4 == 0 && year%100 != 0) {
return true;
}
// 世纪闰年,后两位以00结尾,且能被400整除
if (year%100 == 0 && year%400 == 0) {
return true;
}
return false;
}
小结
算法专题目前已更新LeetCode算法题文章267+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
LeetCode.1154-一年中的第几天(Day of the Year)的更多相关文章
- LeetCode 1154. 一年中的第几天
给你一个按 YYYY-MM-DD 格式表示日期的字符串 date,请你计算并返回该日期是当年的第几天. 通常情况下,我们认为 1 月 1 日是每年的第 1 天,1 月 2 日是每年的第 2 天,依此类 ...
- 【LeetCode】1154. Day of the Year 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计算1月1号之间天数 日期 题目地址:https:// ...
- 【leetcode】1154. Day of the Year
题目如下: Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the ...
- 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)
Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...
- mysql学习 | LeetCode数据库简单查询练习
力扣:https://leetcode-cn.com/ 力扣网数据库练习:https://leetcode-cn.com/problemset/database/ 文章目录 175. 组合两个表 题解 ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
随机推荐
- c++关于字符串的读入和截取
#include<iostream>#include<string>#include<vector>using namespace std;vector<st ...
- 直接插入排序java代码
//直接插入排序(无哨兵) 通过测试 public class InsertSortTest{ public static void insertSort(int[] arr) { for (int ...
- C#验证数字的正则表达
说明:@符号的作用,省去转义字符\ "^\\+?[1-9][0-9]*$" 与 @"^\+?[1-9][0-9]*$" 等效 @"^(0?[1-9 ...
- removeAttr(name)
removeAttr(name) 概述 从每一个匹配的元素中删除一个属性 1.6以下版本在IE6使用JQuery的removeAttr方法删除disabled是无效的.解决的方法就是使用$(" ...
- java-并发编程之fork/join框架
Fork/Join框架是Java 7提供的一个用于并行执行任务的框架,是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架.Fork/Join框架要完成两件事情: 1.任务分 ...
- Eclips的JDK更换为1.8
1.Window—Preferences—Java—Compiler—右侧面板设置为1.6 2.Window—Preferences—Java—Installed JREs—右侧面板“Add”本地的1 ...
- return返回方法值:狮子玩具
public class Lion { String color ="黄色"; public void run(){ System.out.println("正在以0.1 ...
- MessageListenerAdapter--消息监听适配器
我们把之前的消息监听代码注释,可以不用直接加消息监听,而是采用MessageListenerAdapter的方式,我们来学习下如何使用默认的handleMessage,自定义方法名,自定义转换器. 适 ...
- gRPC-Web正式发布
前言: gRPC-Web是一个JavaScript客户端库,可以使Web应用程序直接与后端gRPC服务进行通信,而无需HTTP服务器充当中介. 这意味着可以通过使用.proto文件定义客户端和服务器端 ...
- linux修改ulimit参数
有如下三种修改方式: 1.在/etc/rc.local 中增加一行 ulimit -SHn 655352.在/etc/profile 中增加一行 ulimit -SHn 655353.在/etc/se ...