-
Notifications
You must be signed in to change notification settings - Fork 4
/
1154. Day of the Year.cpp
36 lines (30 loc) · 1014 Bytes
/
1154. 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
class Solution {
public:
bool isLeapYear( int year ) {
if( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) ) {
return true;
}
return false;
}
int monthToDays( int month, int year ) {
if( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ) {
return 31;
} else if( month == 2 ) {
if( isLeapYear(year) ) {
return 29;
}
return 28;
}
return 30;
}
int dayOfYear(string date) {
int month = ( date[5] - '0') * 10 + ( date[6] - '0');
int day = ( date[8] - '0' ) * 10 + ( date[9] - '0' );
int year = ( date[0] - '0' ) * 1000 + ( date[1] - '0' ) * 100 + ( date[2] - '0' ) * 10 + ( date[3] - '0' );
int days = day;
for( int i = 1 ; i < month ; i++ ) {
days += monthToDays(i, year);
}
return days;
}
};