forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-days-between-two-dates.cpp
40 lines (34 loc) · 1.17 KB
/
number-of-days-between-two-dates.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
37
38
39
40
// 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 daysBetweenDates(string date1, string date2) {
return abs(dayOfYear(date1) - dayOfYear(date2));
}
private:
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 (Y - 1) * 365 + ((Y - 1) / 4 - (Y - 1) / 100 + (Y - 1) / 400) + lookup_[M - 1] + D + leap;
}
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_;
};