【小航的算法日记】日期算法

一、概念

闰年(四年一润&&一百年不润||四百年再润)

二、模板

boolean isLeapYear(int year) {
	return (year % 4 == 0 && year % 100 != 0 || (year % 400 == 0);
}

三、例题

题:1185. 一周中的第几天

给你一个日期,请你设计一个算法来判断它是对应一周中的哪一天。

输入为三个整数:daymonthyear,分别表示日、月、年。

您返回的结果必须是这几个值中的一个{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}

示例 1:

输入:day = 31, month = 8, year = 2019
输出:"Saturday"

示例 2:

输入:day = 18, month = 7, year = 1999
输出:"Sunday"

示例 3:

输入:day = 15, month = 8, year = 1993
输出:"Sunday"

提示:

给出的日期一定是在 1971 到 2100 年之间的有效日期。

解:

解题思路:闰年后,逢四年一润

AC代码:

class Solution {
    public String dayOfTheWeek(int day, int month, int year) {
        String[] week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
        int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        // 分别计算年、月、日的贡献,year-1到1971年的天数,1968年是闰年,逢四年一润
        int days =((year - 1) - 1971 + 1) * 365  + ((year - 1) - 1968) / 4;
        for(int i = 0; i < month - 1; i ++) { // 注意year,month都要减去1
            days += monthDays[i];
        }   
        if(month > 2 && ((year % 4  == 0 && year % 100 != 0) || (year % 400 == 0))) {
            days ++;
        }
        days += day;
        return week[(days + 3) % 7];
    }
}

题:1154. 一年中的第几天

给你一个字符串 date ,按 YYYY-MM-DD 格式表示一个 现行公元纪年法 日期。请你计算并返回该日期是当年的第几天。

通常情况下,我们认为 1 月 1 日是每年的第 1 天,1 月 2 日是每年的第 2 天,依此类推。每个月的天数与现行公元纪年法(格里高利历)一致。

示例 1:

输入:date = "2019-01-09"
输出:9

示例 2:

输入:date = "2019-02-10"
输出:41

示例 3:

输入:date = "2003-03-01"
输出:60

示例 4:

输入:date = "2004-03-01"
输出:61

提示:

date.length == 10
date[4] == date[7] == '-',其他的 date[i] 都是数字
date 表示的范围从 1900 年 1 月 1 日至 2019 年 12 月 31 日

解:

解题思路:模拟

AC代码:

class Solution {
    public int dayOfYear(String date) {
        int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        // yyyy-mm-dd
        // 0123-56-89
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        if(month > 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) monthDays[1] = 29;
        int days = Integer.parseInt(date.substring(8));
        for(int i = 0; i < month - 1; i ++) {
            days += monthDays[i];
        }
        return days;
    }
}

题:1360. 日期之间隔几天

请你编写一个程序来计算两个日期之间隔了多少天。

日期以字符串形式给出,格式为 YYYY-MM-DD,如示例所示。

示例 1:

输入:date1 = "2019-06-29", date2 = "2019-06-30"
输出:1

示例 2:

输入:date1 = "2020-01-15", date2 = "2019-12-31"
输出:15

提示:

给定的日期是 1971 年到 2100 年之间的有效日期。

解:

解题思路:模拟

注意:输入的日期没有前后关系,所以要取绝对值

AC代码:

class Solution {
    public int daysBetweenDates(String date1, String date2) {    
        return Math.abs(daysFun(date1) - daysFun(date2));
    }
    // 统计日期距离1971年的日期
    int daysFun(String datestr) {
        int[] dates = dateToInt(datestr);
        int days = 0;
        // year-1 到 1971 的天数,年的贡献
        days += ((dates[0] - 1) - 1971 + 1) * 365 + ((dates[0] - 1) - 1968) / 4;
        // 月的贡献
        int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};       
        for(int i = 0; i < dates[1] - 1; i ++) {
            if(i == 1 && isRun(dates[0])) days += 1;
            days += monthDays[i]; 
        }
        // 日的贡献
        days += dates[2];
        return days;
    }
    // 日期转化
    int[] dateToInt(String date) {
        String[] dates = date.split("-");
        int year = Integer.parseInt(dates[0]);
        int month = Integer.parseInt(dates[1]);
        int day = Integer.parseInt(dates[2]);
        return new int[] {year, month, day};
    }
    // 判断闰年
    boolean isRun(int year) {
        if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return true;
        return false;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值