【LeetCode】1154. 一年中的第几天 + 1185. 一周中的第几天

103 阅读3分钟

题目链接

image.png

Python3

class Solution:
    def dayOfYear(self, date: str) -> int:
        year = int(date[:4])
        month = int(date[5:7])
        day = int(date[8:])

        lis31 = [1, 3,  5, 7, 8, 10, 12]

        res = day 
        for i in range(month - 1, 0, -1):
            if i in lis31:
                res += 31
            elif i == 2 and i != month:
                if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) :
                    res += 29
                else:
                    res += 28
            else:
                res += 30

        return res

官方解法: 打表 O(1)\lgroup O(1) \rgroup

class Solution:
    def dayOfYear(self, date: str) -> int:
        year, month, day = [int(x) for x in date.split("-")]

        amount = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
            amount[1] += 1
        
        return sum(amount[:month-1]) + day

image.png

库函数

# class Solution:
#     def dayOfYear(self, date: str) -> int:
#         return datetime.datetime.strptime(date, "%Y-%m-%d").timetuple().tm_yday

class Solution:
    def dayOfYear(self, date: str) -> int:
        return int((datetime.datetime.strptime(date, "%Y-%m-%d")).strftime('%j'))

C++

class Solution {
public:
    int dayOfYear(string date) {
        int year = stoi(date.substr(0, 4));
        int month = stoi(date.substr(5, 7));
        int day = stoi(date.substr(8, 10));
        
        int amount[] = {31, 28, 31, 30, 31, 30,  31, 31, 30, 31, 30, 31};

        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)){
            ++amount[1];
        }
        int res = 0;
        for (int i = 0; i < month - 1; ++i){
            res += amount[i];
        }
        return res + day;
    }
};

1185. 一周中的第几天

题目链接

image.png

image.png

python3

class Solution:
    def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
        # 算总天数, 除以 7   1970年 12 月 31 日 是 星期四
        days = 0 
        for i in range(1971, year):
            if i % 400 == 0 or (i % 4 == 0 and i % 100 != 0):
                days += 366
            else:
                days += 365
                
        days += self.dayOfYear(day, month, year)
        lis = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
        return lis[(days + 4 ) % 7]  
        
    def dayOfYear(self, day, month, year) -> int:
        lis31 = [1, 3,  5, 7, 8, 10, 12]

        res = day 
        for i in range(month - 1, 0, -1):
            if i in lis31:
                res += 31
            elif i == 2 and i != month:
                if year % 400 == 0 or (year % 100 != 0 and year % 4 == 0) :
                    res += 29
                else:
                    res += 28
            else:
                res += 30

        return res

image.png

官方题解 模拟 O(12)

class Solution:
    def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
        week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
        monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]
        days = 0
        # 输入年份之前的年份的天数贡献
        days += 365 * (year - 1 - 1970) + (year - 1 - 1968) // 4  # 妙啊 # 1968 为 最近的闰年  1967 1966 1965   1964
        # 输入年份中,输入月份之前的月份的天数贡献
        days += sum(monthDays[:month-1])
        if (year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)) and month >= 3:
            days += 1
        # 输入月份中的天数贡献
        days += day

        return week[(days + 3) % 7]

image.png

C++ 模拟 O(12)

class Solution {
public:
    string dayOfTheWeek(int day, int month, int year) {
        vector<string> week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
        vector<int> monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};
        // 计算 year 之前的天数
        int days = 365 * (year - 1 - 1970) + (year - 1 - 1968) / 4;
        // 计算 month 之前的天数
        for (int i = 0; i < month - 1; ++i){
            days += monthDays[i];
        }
        if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && month >= 3){ // 注意月份
            days += 1;
        }
        // 加上 day 
        days += day;
        return week[(days + 3) % 7];
    }
};