Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ts solution to lc problem: No.1185 #2168

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add ts solution to lc problem: No.1185
No.1185.Day of the Week
  • Loading branch information
yanglbme committed Dec 30, 2023
commit 1e3ae892d50e72ef903b8d1ae12c7e7642c10013
38 changes: 35 additions & 3 deletions solution/1100-1199/1185.Day of the Week/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,26 @@

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

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

<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1100-1199/1185.Day%20of%20the%20Week/images/zeller.svg">
我们可以使用蔡勒公式来计算星期几,蔡勒公式如下:

$$
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
$$

其中:

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

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -158,6 +166,30 @@ func dayOfTheWeek(d int, m int, y int) string {
}
```

### **TypeScript**

```ts
function dayOfTheWeek(d: number, m: number, y: number): string {
if (m < 3) {
m += 12;
y -= 1;
}
const c: number = (y / 100) | 0;
y %= 100;
const w = (((c / 4) | 0) - 2 * c + y + ((y / 4) | 0) + (((13 * (m + 1)) / 5) | 0) + d - 1) % 7;
const weeks: string[] = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
return weeks[(w + 7) % 7];
}
```

### **...**

```
Expand Down
44 changes: 42 additions & 2 deletions solution/1100-1199/1185.Day of the Week/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,25 @@

## Solutions

Zeller formula.
**Solution 1: Zeller's Congruence**

<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1100-1199/1185.Day%20of%20the%20Week/images/zeller.svg">
We can use Zeller's Congruence to calculate the day of the week. Zeller's Congruence is as follows:

$$
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
$$

Where:

- `w`: Day of the week (starting from Sunday)
- `c`: First two digits of the year
- `y`: Last two digits of the year
- `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)
- `d`: Day
- `⌊⌋`: Floor function (round down)
- `mod`: Modulo operation

The time complexity is $O(1)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -143,6 +159,30 @@ func dayOfTheWeek(d int, m int, y int) string {
}
```

### **TypeScript**

```ts
function dayOfTheWeek(d: number, m: number, y: number): string {
if (m < 3) {
m += 12;
y -= 1;
}
const c: number = (y / 100) | 0;
y %= 100;
const w = (((c / 4) | 0) - 2 * c + y + ((y / 4) | 0) + (((13 * (m + 1)) / 5) | 0) + d - 1) % 7;
const weeks: string[] = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
return weeks[(w + 7) % 7];
}
```

### **...**

```
Expand Down
19 changes: 19 additions & 0 deletions solution/1100-1199/1185.Day of the Week/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function dayOfTheWeek(d: number, m: number, y: number): string {
if (m < 3) {
m += 12;
y -= 1;
}
const c: number = (y / 100) | 0;
y %= 100;
const w = (((c / 4) | 0) - 2 * c + y + ((y / 4) | 0) + (((13 * (m + 1)) / 5) | 0) + d - 1) % 7;
const weeks: string[] = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
return weeks[(w + 7) % 7];
}