Write a program to count the number of days between two dates.

The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples.

 

Example 1:

Input: date1 = "2019-06-29", date2 = "2019-06-30"
Output: 1
Example 2:

Input: date1 = "2020-01-15", date2 = "2019-12-31"
Output: 15
 

Constraints:

The given dates are valid dates between the years 1971 and 2100.

这题直接硬解了,没用什么算法,就是笨办法。

class NumberOfDaysBetweenTwoDates : public Solution {
public:
    void Exec() {
        auto res = daysBetweenDates("2020-01-15", "2019-12-31");
    }
    int getDaysOfYear(int year) {
        int days = 0;
        // Full
        if (year % 4 == 0) {
            if (year % 100 == 0 && year % 400 != 0) {
                days += 365;
            } else {
                days += 366;
            }
        } else {
            days += 365;
        }
        return days;
    }
    int getDaysOfDate(int year, int month, int day) {
        int days = 0;
        for (int i = 1; i < month; i++) {
            if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
                days += 31;
            } else if (i == 2) {
                if (year % 4 == 0) {
                    if (year % 100 == 0 && year % 400 != 0) {
                        days += 28;
                    } else {
                        days += 29;
                    }
                } else {
                    days += 28;
                }
            } else {
                days += 30;
            }
        }
        return days + day;
    }
    int daysBetweenDates(string date1, string date2) {
        int year1 = 0, month1 = 0, day1 = 0;
        int year2 = 0, month2 = 0, day2 = 0;

        size_t pos = date1.find("-");
        size_t prev = 0;
        year1 = atoi(date1.substr(0, pos - 1 - 0 + 1).c_str());
        prev = pos;
        pos = date1.find("-", pos + 1);
        month1 = atoi(date1.substr(prev + 1, pos - 1 - prev + 1).c_str());
        prev = pos;
        pos = date1.find("-", pos + 1);
        day1 = atoi(date1.substr(prev + 1).c_str());

        pos = date1.find("-");
        prev = 0;
        year2 = atoi(date2.substr(0, pos - 1 - 0 + 1).c_str());
        prev = pos;
        pos = date1.find("-", pos + 1);
        month2 = atoi(date2.substr(prev + 1, pos - 1 - prev + 1).c_str());
        prev = pos;
        pos = date1.find("-", pos + 1);
        day2 = atoi(date2.substr(prev + 1).c_str());

        if (year1 > year2 ||
        (year1 == year2 && month1 > month2) ||
        (year1 == year2 && month1 == month2 && day1 > day2)) {
            std::swap(year1, year2);
            std::swap(month1, month2);
            std::swap(day1, day2);
        }

        if (year1 == year2) {
            return getDaysOfDate(year2, month2, day2) - getDaysOfDate(year1, month1, day1);
        }

        int days = 0;
        for (int year = year1 + 1; year < year2; year++) {
            // Full
            days += getDaysOfYear(year);
        }

        days += getDaysOfYear(year1);
        days -= getDaysOfDate(year1, month1, day1);
        days += getDaysOfDate(year2, month2, day2);
        return days;
    }
};
共 0 条回复
暂时没有人回复哦,赶紧抢沙发
发表新回复

作者

sryan
today is a good day