-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDayOfTheWeek.java
More file actions
39 lines (32 loc) · 1.13 KB
/
DayOfTheWeek.java
File metadata and controls
39 lines (32 loc) · 1.13 KB
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
package math;
/**
* Description: https://leetcode.com/problems/day-of-the-week
* Difficulty: Easy
* Time complexity: O(n)
* Space complexity: O(1)
*/
public class DayOfTheWeek {
private static final int EPOCH_YEAR = 1971;
public String dayOfTheWeek(int day, int month, int year) {
// 1971-01-01 was a Friday
String[] weekDays = new String[]{"Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"};
int epochDay = 0;
for (int y = EPOCH_YEAR; y < year; y++) {
epochDay += isLeapYear(y) ? 366 : 365;
}
// during the leap year February has 29 days
int[] daysPerMonth = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year)) daysPerMonth[1] = 29;
for (int m = 0; m < month - 1; m++) {
epochDay += daysPerMonth[m];
}
// zero indexed
epochDay += day - 1;
return weekDays[epochDay % weekDays.length];
}
private boolean isLeapYear(int year) {
if (year % 400 == 0) return true;
if (year % 100 == 0) return false;
return year % 4 == 0;
}
}