@@ -61,123 +61,15 @@ tags:
61
61
62
62
<!-- solution:start -->
63
63
64
- ### 方法一:双指针
64
+ ### 方法一:枚举
65
65
66
- 利用双指针,找到每一段连续递增子数组的长度,我们记为 ` cnt ` ,每次将 $(1+cnt)\times cnt / 2$ 累加到答案中 。
66
+ 我们可以枚举以每个元素结尾的严格递增子数组的个数,然后将它们累加起来即可 。
67
67
68
- 时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 是数组的长度 。
68
+ 我们用一个变量 $\textit{cnt}$ 来记录以当前元素结尾的严格递增子数组的个数,初始时 $\textit{cnt} = 1$。然后我们从第二个元素开始遍历数组,如果当前元素大于前一个元素,那么 $\textit{cnt}$ 就可以加 $1$,否则 $\textit{cnt}$ 重置为 $1$。此时,以当前元素结尾的严格递增子数组的个数就是 $\textit{cnt}$,我们将其累加到答案中即可 。
69
69
70
- <!-- tabs:start -->
71
-
72
- #### Python3
73
-
74
- ``` python
75
- class Solution :
76
- def countSubarrays (self , nums : List[int ]) -> int :
77
- ans = i = 0
78
- while i < len (nums):
79
- j = i + 1
80
- while j < len (nums) and nums[j] > nums[j - 1 ]:
81
- j += 1
82
- cnt = j - i
83
- ans += (1 + cnt) * cnt // 2
84
- i = j
85
- return ans
86
- ```
87
-
88
- #### Java
89
-
90
- ``` java
91
- class Solution {
92
- public long countSubarrays (int [] nums ) {
93
- long ans = 0 ;
94
- int i = 0 , n = nums. length;
95
- while (i < n) {
96
- int j = i + 1 ;
97
- while (j < n && nums[j] > nums[j - 1 ]) {
98
- ++ j;
99
- }
100
- long cnt = j - i;
101
- ans += (1 + cnt) * cnt / 2 ;
102
- i = j;
103
- }
104
- return ans;
105
- }
106
- }
107
- ```
108
-
109
- #### C++
110
-
111
- ``` cpp
112
- class Solution {
113
- public:
114
- long long countSubarrays(vector<int >& nums) {
115
- long long ans = 0;
116
- int i = 0, n = nums.size();
117
- while (i < n) {
118
- int j = i + 1;
119
- while (j < n && nums[ j] > nums[ j - 1] ) {
120
- ++j;
121
- }
122
- int cnt = j - i;
123
- ans += 1ll * (1 + cnt) * cnt / 2;
124
- i = j;
125
- }
126
- return ans;
127
- }
128
- };
129
- ```
130
-
131
- #### Go
132
-
133
- ```go
134
- func countSubarrays(nums []int) int64 {
135
- ans := 0
136
- i, n := 0, len(nums)
137
- for i < n {
138
- j := i + 1
139
- for j < n && nums[j] > nums[j-1] {
140
- j++
141
- }
142
- cnt := j - i
143
- ans += (1 + cnt) * cnt / 2
144
- i = j
145
- }
146
- return int64(ans)
147
- }
148
- ```
149
-
150
- #### TypeScript
151
-
152
- ``` ts
153
- function countSubarrays(nums : number []): number {
154
- let ans = 0 ;
155
- let i = 0 ;
156
- const n = nums .length ;
157
- while (i < n ) {
158
- let j = i + 1 ;
159
- while (j < n && nums [j ] > nums [j - 1 ]) {
160
- ++ j ;
161
- }
162
- const cnt = j - i ;
163
- ans += ((1 + cnt ) * cnt ) / 2 ;
164
- i = j ;
165
- }
166
- return ans ;
167
- }
168
- ```
169
-
170
- <!-- tabs: end -->
70
+ 遍历结束后,返回答案即可。
171
71
172
- <!-- solution: end -->
173
-
174
- <!-- solution: start -->
175
-
176
- ### 方法二:枚举
177
-
178
- 我们可以枚举数组中的每一个元素,找到以该元素为结尾的严格递增子数组的个数,然后将这些个数累加到答案中。
179
-
180
- 时间复杂度 $O(n)$,空间复杂度 $O(1)$,其中 $n$ 是数组的长度。
72
+ 时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。
181
73
182
74
<!-- tabs:start -->
183
75
@@ -186,13 +78,12 @@ function countSubarrays(nums: number[]): number {
186
78
``` python
187
79
class Solution :
188
80
def countSubarrays (self , nums : List[int ]) -> int :
189
- ans = pre = cnt = 0
190
- for x in nums:
191
- if pre < x :
81
+ ans = cnt = 1
82
+ for x, y in pairwise( nums) :
83
+ if x < y :
192
84
cnt += 1
193
85
else :
194
86
cnt = 1
195
- pre = x
196
87
ans += cnt
197
88
return ans
198
89
```
@@ -202,15 +93,13 @@ class Solution:
202
93
``` java
203
94
class Solution {
204
95
public long countSubarrays (int [] nums ) {
205
- long ans = 0 ;
206
- int pre = 0 , cnt = 0 ;
207
- for (int x : nums) {
208
- if (pre < x) {
96
+ long ans = 1 , cnt = 1 ;
97
+ for (int i = 1 ; i < nums. length; ++ i) {
98
+ if (nums[i - 1 ] < nums[i]) {
209
99
++ cnt;
210
100
} else {
211
101
cnt = 1 ;
212
102
}
213
- pre = x;
214
103
ans += cnt;
215
104
}
216
105
return ans;
@@ -224,16 +113,14 @@ class Solution {
224
113
class Solution {
225
114
public:
226
115
long long countSubarrays(vector<int >& nums) {
227
- long long ans = 0;
228
- int pre = 0, cnt = 0;
229
- for (int x : nums) {
230
- if (pre < x) {
116
+ long long ans = 1, cnt = 1;
117
+ for (int i = 1; i < nums.size(); ++i) {
118
+ if (nums[ i - 1] < nums[ i] ) {
231
119
++cnt;
232
120
} else {
233
121
cnt = 1;
234
122
}
235
123
ans += cnt;
236
- pre = x;
237
124
}
238
125
return ans;
239
126
}
@@ -243,36 +130,32 @@ public:
243
130
#### Go
244
131
245
132
```go
246
- func countSubarrays(nums []int) (ans int64) {
247
- pre , cnt := 0, 0
248
- for _ , x := range nums {
249
- if pre < x {
133
+ func countSubarrays(nums []int) int64 {
134
+ ans , cnt := 1, 1
135
+ for i , x := range nums[1:] {
136
+ if nums[i] < x {
250
137
cnt++
251
138
} else {
252
139
cnt = 1
253
140
}
254
- ans += int64(cnt)
255
- pre = x
141
+ ans += cnt
256
142
}
257
- return
143
+ return int64(ans)
258
144
}
259
145
```
260
146
261
147
#### TypeScript
262
148
263
149
``` ts
264
150
function countSubarrays(nums : number []): number {
265
- let ans = 0 ;
266
- let pre = 0 ;
267
- let cnt = 0 ;
268
- for (const x of nums ) {
269
- if (pre < x ) {
151
+ let [ans, cnt] = [1 , 1 ];
152
+ for (let i = 1 ; i < nums .length ; ++ i ) {
153
+ if (nums [i - 1 ] < nums [i ]) {
270
154
++ cnt ;
271
155
} else {
272
156
cnt = 1 ;
273
157
}
274
158
ans += cnt ;
275
- pre = x ;
276
159
}
277
160
return ans ;
278
161
}
0 commit comments