1154. Day of the Year
https://leetcode.com/problems/day-of-the-year/
Input: date = "2019-01-09"
Output: 9
Explanation: Given date is the 9th day of the year in 2019.Input: date = "2019-02-10"
Output: 41Input: date = "2003-03-01"
Output: 60Input: date = "2004-03-01"
Output: 61class Solution {
public:
int dayOfYear(string date) {
vector<int> days{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, 2));
if (m > 2 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) ++d;
while (--m > 0) d += days[m - 1];
return d;
}
};Last updated