Day of the Week LeetCode solution in Java

1
class Solution {
2
public String dayOfTheWeek(int day, int month, int year) {
3
String[] week = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
4
year--;
5
int total = (year / 4) * 366 + (year - year / 4) * 365;
6
int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
7
year++;
8
if (year % 4 == 0 && year != 2100) {
9
months[1]++;
10
}
11
for (int i = 0; i < month - 1; i++) {
12
total += months[i];
13
}
14
total += day;
15
// for(int i:months){
16
// System.out.print(i+" ");
17
// }
18
// System.out.println();
19
return week[(total - 1) % 7];
20
}
21
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0