How to Compute the Day of the Year?

  • 时间:2020-09-20 14:08:18
  • 分类:网络文摘
  • 阅读:89 次

Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.

Example 1:
Input: date = “2019-01-09”
Output: 9
Explanation: Given date is the 9th day of the year in 2019.

Example 2:
Input: date = “2019-02-10”
Output: 41

Example 3:
Input: date = “2003-03-01”
Output: 60

Example 4:
Input: date = “2004-03-01”
Output: 61

Constraints:
date.length == 10
date[4] == date[7] == ‘-‘, and all other date[i]’s are digits
date represents a calendar date between Jan 1st, 1900 and Dec 31, 2019.

C++ Algorithm to Count the Days Before the Date

To count the days given a date string of ISO 8601 format, we need to first parse/extract the string into three integers: year, month and day.

Then, we need to check if the year is a leap year (which we can use the little function described in How to Determine the Leap Year?).

For February, if it is leap year, we need to add one more day i.e. 29 days in a leap year. For January, March, May, July, August, October, December, there are 31 days, all others (except February which is 28 days in non-leap year), are 30 days.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution {
public:
    int dayOfYear(string date) {
        int year = std::stoi(date.substr(0, 4));
        int month = (date[5] - '0') * 10 +
            date[6] - '0';
        int day = (date[8] - '0') * 10 +
            date[9] - '0';
        int ans = 0;
        for (int i = 1; i < month; ++ i) {
            switch (i) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    ans += 31;
                    break;
                case 2:
                    if (isLeap(year)) {
                        ans += 29;
                    } else {
                        ans += 28;
                    }
                    break;
                default:
                    ans += 30;
            }
        }
        return ans + day;
    }
private:
    bool isLeap(int year) {
        if (year % 400 == 0) return true;
        if (year % 100 == 0) return false;
        if (year % 4 == 0) return true;
        return false;
    }
};
class Solution {
public:
    int dayOfYear(string date) {
        int year = std::stoi(date.substr(0, 4));
        int month = (date[5] - '0') * 10 +
            date[6] - '0';
        int day = (date[8] - '0') * 10 +
            date[9] - '0';
        int ans = 0;
        for (int i = 1; i < month; ++ i) {
            switch (i) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    ans += 31;
                    break;
                case 2:
                    if (isLeap(year)) {
                        ans += 29;
                    } else {
                        ans += 28;
                    }
                    break;
                default:
                    ans += 30;
            }
        }
        return ans + day;
    }
private:
    bool isLeap(int year) {
        if (year % 400 == 0) return true;
        if (year % 100 == 0) return false;
        if (year % 4 == 0) return true;
        return false;
    }
};

Then, the counting should be pretty straightforward, which can be done in O(1) constant, in both time and space.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
数学题:一个直角三角形以它的斜边为轴旋转一周  数学题:一个三角形被一个长方形挡住了  摘桃子的数学题  如图平行四边形ABCD的周长为72厘米  每个人都和其他人握了一次手  客车和货车同时从甲、乙两地的中点反向行驶  数学题:把一个圆锥沿着高切开,得到了个如下图所示的物体  数学题:49个桶,32个扁担,问有几个人挑水,几个人抬水?  学校合唱队有205名学生如果女同学减少25人  两瓶香水甲瓶用去九分之五 
评论列表
添加评论