其他分享
首页 > 其他分享> > 【Leetcode_easy】1154. Day of the Year

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

标签:int,1154,31,30,Day,substr,Year,date,Leetcode
来源: https://www.cnblogs.com/happyamyhope/p/11431176.html