forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-of-the-year.cpp
36 lines (31 loc) · 1011 Bytes
/
day-of-the-year.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Time: O(1)
// Space: O(1)
class Solution {
public:
Solution() : lookup_(12) {
for (int M = 1; M < lookup_.size(); ++M) {
lookup_[M] = lookup_[M - 1] + dayOfMonth(M);
}
}
int dayOfYear(string date) {
const auto& result = split(date, '-');
const auto& Y = stoi(result[0]), &M = stoi(result[1]), &D = stoi(result[2]);
const auto& leap = (M > 2 && (((Y % 4 == 0) && (Y % 100 != 0)) || (Y % 400 == 0))) ? 1 : 0;
return lookup_[M - 1] + D + leap;
}
private:
int dayOfMonth(int M) {
return (M == 2) ? 28 : 31 - (M - 1) % 7 % 2;
}
vector<string> split(const string& s, const char delim) {
vector<string> result;
auto end = string::npos;
do {
const auto& start = end + 1;
end = s.find(delim, start);
result.emplace_back(s.substr(start, end - start));
} while (end != string::npos);
return result;
}
vector<int> lookup_;
};