【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;

posted on 2019-08-29 17:54  鹅要长大  阅读(157)  评论(0)    收藏  举报

导航