Skip to content

Commit fdc1927

Browse files
committed
feat: add solutions to lc problem: No.1185
No.1185.Day of the Week
1 parent 576675f commit fdc1927

File tree

9 files changed

+198
-2
lines changed

9 files changed

+198
-2
lines changed

solution/0700-0799/0703.Kth Largest Element in a Stream/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public:
140140
*/
141141
```
142142
143-
### **TypeScript**
143+
### **JavaScript**
144144
145145
```js
146146
/**

solution/0700-0799/0703.Kth Largest Element in a Stream/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class KthLargest {
102102
*/
103103
```
104104

105-
### **TypeScript**
105+
### **JavaScript**
106106

107107
```js
108108
/**

solution/1100-1199/1185.Day of the Week/README.md

+87
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,109 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

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+
4759
<!-- tabs:start -->
4860

4961
### **Python3**
5062

5163
<!-- 这里可写当前语言的特殊实现逻辑 -->
5264

5365
```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+
```
5470

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]
5581
```
5682

5783
### **Java**
5884

5985
<!-- 这里可写当前语言的特殊实现逻辑 -->
6086

6187
```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+
```
62135
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+
}
63150
```
64151

65152
### **...**

solution/1100-1199/1185.Day of the Week/README_EN.md

+79
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,97 @@
4141

4242
## Solutions
4343

44+
Zeller formula.
45+
46+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1100-1199/1185.Day%20of%20the%20Week/images/zeller.svg">
47+
4448
<!-- tabs:start -->
4549

4650
### **Python3**
4751

4852
```python
53+
class Solution:
54+
def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
55+
return datetime.date(year, month, day).strftime('%A')
56+
```
4957

58+
```python
59+
class Solution:
60+
def dayOfTheWeek(self, d: int, m: int, y: int) -> str:
61+
if m < 3:
62+
m += 12
63+
y -= 1
64+
c = y // 100
65+
y = y % 100
66+
w = (c // 4 - 2 * c + y + y // 4 + 13 * (m + 1) // 5 + d - 1) % 7
67+
return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][w]
5068
```
5169

5270
### **Java**
5371

5472
```java
73+
import java.util.Calendar;
74+
75+
class Solution {
76+
private static final String[] WEEK = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
77+
78+
public static String dayOfTheWeek(int day, int month, int year) {
79+
Calendar calendar = Calendar.getInstance();
80+
calendar.set(year, month - 1, day);
81+
return WEEK[calendar.get(Calendar.DAY_OF_WEEK) - 1];
82+
}
83+
}
84+
```
85+
86+
```java
87+
class Solution {
88+
public String dayOfTheWeek(int d, int m, int y) {
89+
if (m < 3) {
90+
m += 12;
91+
y -= 1;
92+
}
93+
int c = y / 100;
94+
y %= 100;
95+
int w = (c / 4 - 2 * c + y + y / 4 + 13 * (m + 1) / 5 + d - 1) % 7;
96+
return new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}[(w + 7) % 7];
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
string dayOfTheWeek(int d, int m, int y) {
107+
if (m < 3)
108+
{
109+
m += 12;
110+
y -= 1;
111+
}
112+
int c = y / 100;
113+
y %= 100;
114+
int w = (c / 4 - 2 * c + y + y / 4 + 13 * (m + 1) / 5 + d - 1) % 7;
115+
vector<string> weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
116+
return weeks[(w + 7) % 7];
117+
}
118+
};
119+
```
55120
121+
### **Go**
122+
123+
```go
124+
func dayOfTheWeek(d int, m int, y int) string {
125+
if m < 3 {
126+
m += 12
127+
y -= 1
128+
}
129+
c := y / 100
130+
y %= 100
131+
w := (c/4 - 2*c + y + y/4 + 13*(m+1)/5 + d - 1) % 7
132+
weeks := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
133+
return weeks[(w+7)%7]
134+
}
56135
```
57136

58137
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
string dayOfTheWeek(int d, int m, int y) {
4+
if (m < 3)
5+
{
6+
m += 12;
7+
y -= 1;
8+
}
9+
int c = y / 100;
10+
y %= 100;
11+
int w = (c / 4 - 2 * c + y + y / 4 + 13 * (m + 1) / 5 + d - 1) % 7;
12+
vector<string> weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
13+
return weeks[(w + 7) % 7];
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func dayOfTheWeek(d int, m int, y int) string {
2+
if m < 3 {
3+
m += 12
4+
y -= 1
5+
}
6+
c := y / 100
7+
y %= 100
8+
w := (c/4 - 2*c + y + y/4 + 13*(m+1)/5 + d - 1) % 7
9+
weeks := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
10+
return weeks[(w+7)%7]
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
3+
return datetime.date(year, month, day).strftime('%A')
Loading

0 commit comments

Comments
 (0)