Knowing
1/1/1971
isFriday
(4
in array), sum up all days to today.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
28int[][] monthDay = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
};
public String dayOfTheWeek(int day, int month, int year) {
// 1/1/1971 Friday 4
int res = 4;
for (int i = 1971; i < year; ++i) {
int k = isLeap(i);
for (int j = 1; j <= 12; ++j) res += monthDay[k][j];
}
int k = isLeap(year);
for (int i = 1; i < month; ++i) {
res += monthDay[k][i];
}
res += day;
res %= 7;
String[] A = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
return A[res];
}
public int isLeap(int year) {
return ((year %4 == 0 && year % 100 != 0) || year %400 == 0) ? 1 : 0;
}坂本友彦:
1 | class Solution { |