File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Time: O(1)
2
+ // Space: O(1)
3
+
4
+ class Solution {
5
+ public:
6
+ Solution () : lookup_(12 ) {
7
+ for (int M = 1 ; M < lookup_.size (); ++M) {
8
+ lookup_[M] = lookup_[M - 1 ] + dayOfMonth (M);
9
+ }
10
+ }
11
+
12
+ int dayOfYear (string date) {
13
+ const auto & result = split (date, ' -' );
14
+ const auto & Y = stoi (result[0 ]), &M = stoi (result[1 ]), &D = stoi (result[2 ]);
15
+ const auto & leap = (M > 2 && (((Y % 4 == 0 ) && (Y % 100 != 0 )) || (Y % 400 == 0 ))) ? 1 : 0 ;
16
+ return lookup_[M - 1 ] + D + leap;
17
+ }
18
+
19
+ private:
20
+ int dayOfMonth (int M) {
21
+ return (M == 2 ) ? 28 : 31 - (M - 1 ) % 7 % 2 ;
22
+ }
23
+
24
+ vector<string> split (const string& s, const char delim) {
25
+ vector<string> result;
26
+ auto end = string::npos;
27
+ do {
28
+ const auto & start = end + 1 ;
29
+ end = s.find (delim, start);
30
+ result.emplace_back (s.substr (start, end - start));
31
+ } while (end != string::npos);
32
+ return result;
33
+ }
34
+
35
+ vector<int > lookup_;
36
+ };
You can’t perform that action at this time.
0 commit comments