File tree 4 files changed +78
-1
lines changed
1111.Maximum Nesting Depth of Two Valid Parentheses Strings
1118.Number of Days in a Month
4 files changed +78
-1
lines changed Original file line number Diff line number Diff line change 90
90
91
91
我们用一个变量 $x$ 维护当前括号的平衡度,也就是左括号的数量减去右括号的数量。
92
92
93
- 遍历字符串 $seq$,更新 $x$ 的值。如果 $x$ 为偶数 ,我们将当前的左括号分给 $A$,否则分给 $B$。
93
+ 遍历字符串 $seq$,更新 $x$ 的值。如果 $x$ 为奇数 ,我们将当前的左括号分给 $A$,否则分给 $B$。
94
94
95
95
时间复杂度 $O(n)$,其中 $n$ 是字符串 $seq$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
96
96
Original file line number Diff line number Diff line change 44
44
45
45
<!-- 这里可写通用的实现逻辑 -->
46
46
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
+
47
57
<!-- tabs:start -->
48
58
49
59
### ** Python3**
@@ -99,6 +109,30 @@ func numberOfDays(year int, month int) int {
99
109
}
100
110
```
101
111
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
+
102
136
### ** ...**
103
137
104
138
```
Original file line number Diff line number Diff line change @@ -78,6 +78,30 @@ func numberOfDays(year int, month int) int {
78
78
}
79
79
```
80
80
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
+
81
105
### ** ...**
82
106
83
107
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments