【Leetcode_easy】1154. Day of the Year

problem

1154. Day of the Year

solution

class Solution {
public:
    int dayOfYear(string date) {
        // 平年 闰年
        int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int y = stoi(date.substr(0, 4)), m = stoi(date.substr(5, 2)), d = stoi(date.substr(8));
        if(m>2  && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) d++;//leap year..errr..
        while(--m) d += days[m-1];//errr..
        return d;
    }
};

参考

1. Leetcode_easy_1154. Day of the Year;

原文地址:https://www.cnblogs.com/happyamyhope/p/11431176.html