Skip to content

Commit 6e8fe51

Browse files
committed
feat: add solutions to lc problem: No.1118
No.1118.Number of Days in a Month
1 parent acd96e9 commit 6e8fe51

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

solution/1100-1199/1111.Maximum Nesting Depth of Two Valid Parentheses Strings/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090

9191
我们用一个变量 $x$ 维护当前括号的平衡度,也就是左括号的数量减去右括号的数量。
9292

93-
遍历字符串 $seq$,更新 $x$ 的值。如果 $x$ 为偶数,我们将当前的左括号分给 $A$,否则分给 $B$。
93+
遍历字符串 $seq$,更新 $x$ 的值。如果 $x$ 为奇数,我们将当前的左括号分给 $A$,否则分给 $B$。
9494

9595
时间复杂度 $O(n)$,其中 $n$ 是字符串 $seq$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
9696

solution/1100-1199/1118.Number of Days in a Month/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@
4444

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

47+
**方法一:判断闰年**
48+
49+
我们可以先判断给定的年份是否为闰年,如果年份能被 $4$ 整除但不能被 $100$ 整除,或者能被 $400$ 整除,那么这一年就是闰年。
50+
51+
闰年的二月有 $29$ 天,平年的二月有 $28$ 天。
52+
53+
我们可以用一个数组 $days$ 存储当前年份每个月的天数,其中 $days[0]=0$,$days[i]$ 表示当前年份第 $i$ 个月的天数。那么答案就是 $days[month]$。
54+
55+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
56+
4757
<!-- tabs:start -->
4858

4959
### **Python3**
@@ -99,6 +109,30 @@ func numberOfDays(year int, month int) int {
99109
}
100110
```
101111

112+
### **TypeScript**
113+
114+
```ts
115+
function numberOfDays(year: number, month: number): number {
116+
const leap = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
117+
const days = [
118+
0,
119+
31,
120+
leap ? 29 : 28,
121+
31,
122+
30,
123+
31,
124+
30,
125+
31,
126+
31,
127+
30,
128+
31,
129+
30,
130+
31,
131+
];
132+
return days[month];
133+
}
134+
```
135+
102136
### **...**
103137

104138
```

solution/1100-1199/1118.Number of Days in a Month/README_EN.md

+24
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,30 @@ func numberOfDays(year int, month int) int {
7878
}
7979
```
8080

81+
### **TypeScript**
82+
83+
```ts
84+
function numberOfDays(year: number, month: number): number {
85+
const leap = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
86+
const days = [
87+
0,
88+
31,
89+
leap ? 29 : 28,
90+
31,
91+
30,
92+
31,
93+
30,
94+
31,
95+
31,
96+
30,
97+
31,
98+
30,
99+
31,
100+
];
101+
return days[month];
102+
}
103+
```
104+
81105
### **...**
82106

83107
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function numberOfDays(year: number, month: number): number {
2+
const leap = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
3+
const days = [
4+
0,
5+
31,
6+
leap ? 29 : 28,
7+
31,
8+
30,
9+
31,
10+
30,
11+
31,
12+
31,
13+
30,
14+
31,
15+
30,
16+
31,
17+
];
18+
return days[month];
19+
}

0 commit comments

Comments
 (0)