forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
22 lines (22 loc) · 818 Bytes
/
s1.cpp
File metadata and controls
22 lines (22 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// OJ: https://leetcode.com/problems/day-of-the-week/
// Author: github.com/lzl124631x
// Time: O(1)
// Space: O(1)
class Solution {
private:
bool isLeapYear(int y) {
return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
}
string dates[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
public:
string dayOfTheWeek(int day, int month, int year) { // Compare it with 1971/1/1 (Friday)
int d = 0;
for (int y = 1971; y < year; ++y) d += 365 + (isLeapYear(y) ? 1 : 0);
for (int m = 1; m < month; ++m) {
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10) d += 31;
else if (m == 2) d += 28 + (isLeapYear(year) ? 1 : 0);
else d += 30;
}
return dates[(d + day + 4) % 7];
}
};