|
44 | 44 |
|
45 | 45 | <!-- 这里可写通用的实现逻辑 -->
|
46 | 46 |
|
| 47 | +直接调库或者应用蔡勒公式。 |
| 48 | + |
| 49 | +<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1100-1199/1185.Day%20of%20the%20Week/images/zeller.svg"> |
| 50 | + |
| 51 | +- `w`: 星期(从 Sunday 开始) |
| 52 | +- `c`: 年份前两位 |
| 53 | +- `y`: 年份后两位 |
| 54 | +- `m`: 月(m 的取值范围是 3 至 14,即在蔡勒公式中,某年的 1、2 月要看作上一年的 13、14 月来计算,比如 2003 年 1 月 1 日要看作 2002 年的 13 月 1 日来计算) |
| 55 | +- `d`: 日 |
| 56 | +- `[ ]`: 向下取整 |
| 57 | +- `mod`: 取余 |
| 58 | + |
47 | 59 | <!-- tabs:start -->
|
48 | 60 |
|
49 | 61 | ### **Python3**
|
50 | 62 |
|
51 | 63 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
52 | 64 |
|
53 | 65 | ```python
|
| 66 | +class Solution: |
| 67 | + def dayOfTheWeek(self, day: int, month: int, year: int) -> str: |
| 68 | + return datetime.date(year, month, day).strftime('%A') |
| 69 | +``` |
54 | 70 |
|
| 71 | +```python |
| 72 | +class Solution: |
| 73 | + def dayOfTheWeek(self, d: int, m: int, y: int) -> str: |
| 74 | + if m < 3: |
| 75 | + m += 12 |
| 76 | + y -= 1 |
| 77 | + c = y // 100 |
| 78 | + y = y % 100 |
| 79 | + w = (c // 4 - 2 * c + y + y // 4 + 13 * (m + 1) // 5 + d - 1) % 7 |
| 80 | + return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][w] |
55 | 81 | ```
|
56 | 82 |
|
57 | 83 | ### **Java**
|
58 | 84 |
|
59 | 85 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
60 | 86 |
|
61 | 87 | ```java
|
| 88 | +import java.util.Calendar; |
| 89 | + |
| 90 | +class Solution { |
| 91 | + private static final String[] WEEK = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; |
| 92 | + |
| 93 | + public static String dayOfTheWeek(int day, int month, int year) { |
| 94 | + Calendar calendar = Calendar.getInstance(); |
| 95 | + calendar.set(year, month - 1, day); |
| 96 | + return WEEK[calendar.get(Calendar.DAY_OF_WEEK) - 1]; |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +```java |
| 102 | +class Solution { |
| 103 | + public String dayOfTheWeek(int d, int m, int y) { |
| 104 | + if (m < 3) { |
| 105 | + m += 12; |
| 106 | + y -= 1; |
| 107 | + } |
| 108 | + int c = y / 100; |
| 109 | + y %= 100; |
| 110 | + int w = (c / 4 - 2 * c + y + y / 4 + 13 * (m + 1) / 5 + d - 1) % 7; |
| 111 | + return new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}[(w + 7) % 7]; |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +### **C++** |
| 117 | + |
| 118 | +```cpp |
| 119 | +class Solution { |
| 120 | +public: |
| 121 | + string dayOfTheWeek(int d, int m, int y) { |
| 122 | + if (m < 3) |
| 123 | + { |
| 124 | + m += 12; |
| 125 | + y -= 1; |
| 126 | + } |
| 127 | + int c = y / 100; |
| 128 | + y %= 100; |
| 129 | + int w = (c / 4 - 2 * c + y + y / 4 + 13 * (m + 1) / 5 + d - 1) % 7; |
| 130 | + vector<string> weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; |
| 131 | + return weeks[(w + 7) % 7]; |
| 132 | + } |
| 133 | +}; |
| 134 | +``` |
62 | 135 |
|
| 136 | +### **Go** |
| 137 | +
|
| 138 | +```go |
| 139 | +func dayOfTheWeek(d int, m int, y int) string { |
| 140 | + if m < 3 { |
| 141 | + m += 12 |
| 142 | + y -= 1 |
| 143 | + } |
| 144 | + c := y / 100 |
| 145 | + y %= 100 |
| 146 | + w := (c/4 - 2*c + y + y/4 + 13*(m+1)/5 + d - 1) % 7 |
| 147 | + weeks := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} |
| 148 | + return weeks[(w+7)%7] |
| 149 | +} |
63 | 150 | ```
|
64 | 151 |
|
65 | 152 | ### **...**
|
|
0 commit comments