1 2 3 4 5 6 7 8 9 10 11 12 13
| class Solution { public: int dayOfYear(string date) { int dayCount[]{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; int year = atoi(date.substr(0, 4).c_str()); int month = atoi(date.substr(5, 2).c_str()); int day = atoi(date.substr(8, 2).c_str()); int ret = dayCount[month - 1] + day; if(month > 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) ++ret; return ret; } };
|