-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem-1154.cpp
More file actions
31 lines (28 loc) · 915 Bytes
/
Problem-1154.cpp
File metadata and controls
31 lines (28 loc) · 915 Bytes
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
//Problem - 1154
// https://leetcode.com/problems/ordinal-number-of-date/
// O(n) solution passes all tc
class Solution {
public:
int dayOfYear(string date) {
vector <int> month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int year = 0, mon = 0, day = 0, ans = 0;
for(int i = 0; i < 4; i++)
year = year * 10 + (date[i] - '0');
for(int i = 5; i < 7; i++)
mon = mon * 10 + (date[i] - '0');
for(int i = 8; i < 10; i++)
day = day * 10 + (date[i] - '0');
if (year % 4 == 0) {
if (year % 100 == 0)
{
if (year % 400 == 0)
month[1] = 29;
}
else
month[1] = 29;
}
//cout << year << " " << mon << " " << day;
ans = accumulate(month.begin(), month.begin() + (mon-1), 0) + day;
return ans;
}
};