Skip to content

Commit af3c3c7

Browse files
authored
feat: add ts solution to lc problem: No.1185 (#2168)
1 parent d109613 commit af3c3c7

File tree

3 files changed

+96
-5
lines changed

3 files changed

+96
-5
lines changed

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

+35-3
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,26 @@
4444

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

47-
直接调库或者应用蔡勒公式。
47+
**方法一:蔡勒公式**
4848

49-
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1100-1199/1185.Day%20of%20the%20Week/images/zeller.svg">
49+
我们可以使用蔡勒公式来计算星期几,蔡勒公式如下:
50+
51+
$$
52+
w = (\left \lfloor \frac{c}{4} \right \rfloor - 2c + y + \left \lfloor \frac{y}{4} \right \rfloor + \left \lfloor \frac{13(m+1)}{5} \right \rfloor + d - 1) \bmod 7
53+
$$
54+
55+
其中:
5056

5157
- `w`: 星期(从 Sunday 开始)
5258
- `c`: 年份前两位
5359
- `y`: 年份后两位
5460
- `m`: 月(m 的取值范围是 3 至 14,即在蔡勒公式中,某年的 1、2 月要看作上一年的 13、14 月来计算,比如 2003 年 1 月 1 日要看作 2002 年的 13 月 1 日来计算)
5561
- `d`: 日
56-
- `[ ]`: 向下取整
62+
- `⌊⌋`: 向下取整
5763
- `mod`: 取余
5864

65+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
66+
5967
<!-- tabs:start -->
6068

6169
### **Python3**
@@ -158,6 +166,30 @@ func dayOfTheWeek(d int, m int, y int) string {
158166
}
159167
```
160168

169+
### **TypeScript**
170+
171+
```ts
172+
function dayOfTheWeek(d: number, m: number, y: number): string {
173+
if (m < 3) {
174+
m += 12;
175+
y -= 1;
176+
}
177+
const c: number = (y / 100) | 0;
178+
y %= 100;
179+
const w = (((c / 4) | 0) - 2 * c + y + ((y / 4) | 0) + (((13 * (m + 1)) / 5) | 0) + d - 1) % 7;
180+
const weeks: string[] = [
181+
'Sunday',
182+
'Monday',
183+
'Tuesday',
184+
'Wednesday',
185+
'Thursday',
186+
'Friday',
187+
'Saturday',
188+
];
189+
return weeks[(w + 7) % 7];
190+
}
191+
```
192+
161193
### **...**
162194

163195
```

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

+42-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,25 @@
4141

4242
## Solutions
4343

44-
Zeller formula.
44+
**Solution 1: Zeller's Congruence**
4545

46-
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1100-1199/1185.Day%20of%20the%20Week/images/zeller.svg">
46+
We can use Zeller's Congruence to calculate the day of the week. Zeller's Congruence is as follows:
47+
48+
$$
49+
w = (\left \lfloor \frac{c}{4} \right \rfloor - 2c + y + \left \lfloor \frac{y}{4} \right \rfloor + \left \lfloor \frac{13(m+1)}{5} \right \rfloor + d - 1) \bmod 7
50+
$$
51+
52+
Where:
53+
54+
- `w`: Day of the week (starting from Sunday)
55+
- `c`: First two digits of the year
56+
- `y`: Last two digits of the year
57+
- `m`: Month (the range of m is from 3 to 14, that is, in Zeller's Congruence, January and February of a certain year are considered as the 13th and 14th month of the previous year. For example, January 1, 2003 is considered as the 1st day of the 13th month of 2002)
58+
- `d`: Day
59+
- `⌊⌋`: Floor function (round down)
60+
- `mod`: Modulo operation
61+
62+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
4763

4864
<!-- tabs:start -->
4965

@@ -143,6 +159,30 @@ func dayOfTheWeek(d int, m int, y int) string {
143159
}
144160
```
145161

162+
### **TypeScript**
163+
164+
```ts
165+
function dayOfTheWeek(d: number, m: number, y: number): string {
166+
if (m < 3) {
167+
m += 12;
168+
y -= 1;
169+
}
170+
const c: number = (y / 100) | 0;
171+
y %= 100;
172+
const w = (((c / 4) | 0) - 2 * c + y + ((y / 4) | 0) + (((13 * (m + 1)) / 5) | 0) + d - 1) % 7;
173+
const weeks: string[] = [
174+
'Sunday',
175+
'Monday',
176+
'Tuesday',
177+
'Wednesday',
178+
'Thursday',
179+
'Friday',
180+
'Saturday',
181+
];
182+
return weeks[(w + 7) % 7];
183+
}
184+
```
185+
146186
### **...**
147187

148188
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function dayOfTheWeek(d: number, m: number, y: number): string {
2+
if (m < 3) {
3+
m += 12;
4+
y -= 1;
5+
}
6+
const c: number = (y / 100) | 0;
7+
y %= 100;
8+
const w = (((c / 4) | 0) - 2 * c + y + ((y / 4) | 0) + (((13 * (m + 1)) / 5) | 0) + d - 1) % 7;
9+
const weeks: string[] = [
10+
'Sunday',
11+
'Monday',
12+
'Tuesday',
13+
'Wednesday',
14+
'Thursday',
15+
'Friday',
16+
'Saturday',
17+
];
18+
return weeks[(w + 7) % 7];
19+
}

0 commit comments

Comments
 (0)