Skip to content

Commit 880098b

Browse files
committed
feat: add solutions to lc problems: No.0413,2348
* No.0413.Arithmetic Slices * No.2348.Number of Zero-Filled Subarrays
1 parent e1cda29 commit 880098b

File tree

15 files changed

+314
-212
lines changed

15 files changed

+314
-212
lines changed

solution/0400-0499/0413.Arithmetic Slices/README.md

Lines changed: 91 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,22 @@
5050

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

53-
动态规划法。
53+
**方法一:遍历计数**
5454

55-
`dp[i]` 表示以 i 结尾的数组构成的等差数列的个数
55+
我们用 $d$ 表示当前相邻两个元素的差值,用 $cnt$ 表示当前等差数列的长度,初始时 $d = 3000$, $cnt = 2$
5656

57-
如果 `nums[i] + nums[i - 2] ≠ nums[i - 1] * 2`,说明以 i 结尾的数组无法构成等差数列,`dp[i] = 0`;否则 `dp[i] = 1 + dp[i - 1]`
57+
遍历数组 `nums`,对于相邻的两个元素 $a$ 和 $b$,如果 $b - a = d$,则说明当前元素 $b$ 也属于当前等差数列,此时 $cnt$ 自增 1;否则说明当前元素 $b$ 不属于当前等差数列,此时更新 $d = b - a$,$cnt = 2$。如果 $cnt \ge 3$,则说明当前等差数列的长度至少为 3,此时等差数列的个数为 $cnt - 2$,将其加到答案中
5858

59-
结果返回 dp 数组所有元素之和即可。
59+
遍历结束后,即可得到答案。
60+
61+
在代码实现上,我们也可以将 $cnt$ 初始化为 $0$,重置 $cnt$ 时,直接将 $cnt$ 置为 $0$;累加答案时,直接累加 $cnt$ 即可。
62+
63+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是数组 `nums` 的长度。
64+
65+
相似题目:
66+
67+
- [1513. 仅含 1 的子串数](/solution/1500-1599/1513.Number%20of%20Substrings%20With%20Only%201s/README.md)
68+
- [2348. 全 0 子数组的数目](/solution/2300-2399/2348.Number%20of%20Zero-Filled%20Subarrays/README.md)
6069

6170
<!-- tabs:start -->
6271

@@ -67,12 +76,31 @@
6776
```python
6877
class Solution:
6978
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
70-
n = len(nums)
71-
dp = [0] * n
72-
for i in range(2, n):
73-
if nums[i] + nums[i - 2] == (nums[i - 1] << 1):
74-
dp[i] = 1 + dp[i - 1]
75-
return sum(dp)
79+
ans, cnt = 0, 2
80+
d = 3000
81+
for a, b in pairwise(nums):
82+
if b - a == d:
83+
cnt += 1
84+
else:
85+
d = b - a
86+
cnt = 2
87+
ans += max(0, cnt - 2)
88+
return ans
89+
```
90+
91+
```python
92+
class Solution:
93+
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
94+
ans = cnt = 0
95+
d = 3000
96+
for a, b in pairwise(nums):
97+
if b - a == d:
98+
cnt += 1
99+
else:
100+
d = b - a
101+
cnt = 0
102+
ans += cnt
103+
return ans
76104
```
77105

78106
### **Java**
@@ -82,18 +110,18 @@ class Solution:
82110
```java
83111
class Solution {
84112
public int numberOfArithmeticSlices(int[] nums) {
85-
int n = nums.length;
86-
int[] dp = new int[n];
87-
for (int i = 2; i < n; ++i) {
88-
if (nums[i] + nums[i - 2] == (nums[i - 1] << 1)) {
89-
dp[i] = 1 + dp[i - 1];
113+
int ans = 0, cnt = 0;
114+
int d = 3000;
115+
for (int i = 0; i < nums.length - 1; ++i) {
116+
if (nums[i + 1] - nums[i] == d) {
117+
++cnt;
118+
} else {
119+
d = nums[i + 1] - nums[i];
120+
cnt = 0;
90121
}
122+
ans += cnt;
91123
}
92-
int res = 0;
93-
for (int e : dp) {
94-
res += e;
95-
}
96-
return res;
124+
return ans;
97125
}
98126
}
99127
```
@@ -104,38 +132,60 @@ class Solution {
104132
class Solution {
105133
public:
106134
int numberOfArithmeticSlices(vector<int>& nums) {
107-
int n = nums.size();
108-
vector<int> dp(n, 0);
109-
for (int i = 2; i < n; ++i) {
110-
if (nums[i] + nums[i - 2] == (nums[i - 1] * 2)) {
111-
dp[i] = 1 + dp[i - 1];
135+
int ans = 0, cnt = 0;
136+
int d = 3000;
137+
for (int i = 0; i < nums.size() - 1; ++i) {
138+
if (nums[i + 1] - nums[i] == d) {
139+
++cnt;
140+
} else {
141+
d = nums[i + 1] - nums[i];
142+
cnt = 0;
112143
}
144+
ans += cnt;
113145
}
114-
int res = 0;
115-
for (auto e : dp) {
116-
res += e;
117-
}
118-
return res;
146+
return ans;
119147
}
120148
};
121149
```
122150
123151
### **Go**
124152
125153
```go
126-
func numberOfArithmeticSlices(nums []int) int {
127-
n := len(nums)
128-
dp := make([]int, n)
129-
for i := 2; i < n; i++ {
130-
if nums[i]-nums[i-1] == nums[i-1]-nums[i-2] {
131-
dp[i] = 1 + dp[i-1]
154+
func numberOfArithmeticSlices(nums []int) (ans int) {
155+
cnt, d := 0, 3000
156+
for i, b := range nums[1:] {
157+
a := nums[i]
158+
if b-a == d {
159+
cnt++
160+
} else {
161+
d = b - a
162+
cnt = 0
132163
}
164+
ans += cnt
133165
}
134-
res := 0
135-
for _, e := range dp {
136-
res += e
137-
}
138-
return res
166+
return
167+
}
168+
```
169+
170+
### **TypeScript**
171+
172+
```ts
173+
function numberOfArithmeticSlices(nums: number[]): number {
174+
let ans = 0;
175+
let cnt = 0;
176+
let d = 3000;
177+
for (let i = 0; i < nums.length - 1; ++i) {
178+
const a = nums[i];
179+
const b = nums[i + 1];
180+
if (b - a == d) {
181+
++cnt;
182+
} else {
183+
d = b - a;
184+
cnt = 0;
185+
}
186+
ans += cnt;
187+
}
188+
return ans;
139189
}
140190
```
141191

solution/0400-0499/0413.Arithmetic Slices/README_EN.md

Lines changed: 78 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -40,40 +40,57 @@
4040

4141
## Solutions
4242

43-
Dynamic programming.
44-
4543
<!-- tabs:start -->
4644

4745
### **Python3**
4846

4947
```python
5048
class Solution:
5149
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
52-
n = len(nums)
53-
dp = [0] * n
54-
for i in range(2, n):
55-
if nums[i] + nums[i - 2] == (nums[i - 1] << 1):
56-
dp[i] = 1 + dp[i - 1]
57-
return sum(dp)
50+
ans, cnt = 0, 2
51+
d = 3000
52+
for a, b in pairwise(nums):
53+
if b - a == d:
54+
cnt += 1
55+
else:
56+
d = b - a
57+
cnt = 2
58+
ans += max(0, cnt - 2)
59+
return ans
60+
```
61+
62+
```python
63+
class Solution:
64+
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
65+
ans = cnt = 0
66+
d = 3000
67+
for a, b in pairwise(nums):
68+
if b - a == d:
69+
cnt += 1
70+
else:
71+
d = b - a
72+
cnt = 0
73+
ans += cnt
74+
return ans
5875
```
5976

6077
### **Java**
6178

6279
```java
6380
class Solution {
6481
public int numberOfArithmeticSlices(int[] nums) {
65-
int n = nums.length;
66-
int[] dp = new int[n];
67-
for (int i = 2; i < n; ++i) {
68-
if (nums[i] + nums[i - 2] == (nums[i - 1] << 1)) {
69-
dp[i] = 1 + dp[i - 1];
82+
int ans = 0, cnt = 0;
83+
int d = 3000;
84+
for (int i = 0; i < nums.length - 1; ++i) {
85+
if (nums[i + 1] - nums[i] == d) {
86+
++cnt;
87+
} else {
88+
d = nums[i + 1] - nums[i];
89+
cnt = 0;
7090
}
91+
ans += cnt;
7192
}
72-
int res = 0;
73-
for (int e : dp) {
74-
res += e;
75-
}
76-
return res;
93+
return ans;
7794
}
7895
}
7996
```
@@ -84,38 +101,60 @@ class Solution {
84101
class Solution {
85102
public:
86103
int numberOfArithmeticSlices(vector<int>& nums) {
87-
int n = nums.size();
88-
vector<int> dp(n, 0);
89-
for (int i = 2; i < n; ++i) {
90-
if (nums[i] + nums[i - 2] == (nums[i - 1] * 2)) {
91-
dp[i] = 1 + dp[i - 1];
104+
int ans = 0, cnt = 0;
105+
int d = 3000;
106+
for (int i = 0; i < nums.size() - 1; ++i) {
107+
if (nums[i + 1] - nums[i] == d) {
108+
++cnt;
109+
} else {
110+
d = nums[i + 1] - nums[i];
111+
cnt = 0;
92112
}
113+
ans += cnt;
93114
}
94-
int res = 0;
95-
for (auto e : dp) {
96-
res += e;
97-
}
98-
return res;
115+
return ans;
99116
}
100117
};
101118
```
102119
103120
### **Go**
104121
105122
```go
106-
func numberOfArithmeticSlices(nums []int) int {
107-
n := len(nums)
108-
dp := make([]int, n)
109-
for i := 2; i < n; i++ {
110-
if nums[i]-nums[i-1] == nums[i-1]-nums[i-2] {
111-
dp[i] = 1 + dp[i-1]
123+
func numberOfArithmeticSlices(nums []int) (ans int) {
124+
cnt, d := 0, 3000
125+
for i, b := range nums[1:] {
126+
a := nums[i]
127+
if b-a == d {
128+
cnt++
129+
} else {
130+
d = b - a
131+
cnt = 0
112132
}
133+
ans += cnt
113134
}
114-
res := 0
115-
for _, e := range dp {
116-
res += e
117-
}
118-
return res
135+
return
136+
}
137+
```
138+
139+
### **TypeScript**
140+
141+
```ts
142+
function numberOfArithmeticSlices(nums: number[]): number {
143+
let ans = 0;
144+
let cnt = 0;
145+
let d = 3000;
146+
for (let i = 0; i < nums.length - 1; ++i) {
147+
const a = nums[i];
148+
const b = nums[i + 1];
149+
if (b - a == d) {
150+
++cnt;
151+
} else {
152+
d = b - a;
153+
cnt = 0;
154+
}
155+
ans += cnt;
156+
}
157+
return ans;
119158
}
120159
```
121160

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
class Solution {
22
public:
33
int numberOfArithmeticSlices(vector<int>& nums) {
4-
int n = nums.size();
5-
vector<int> dp(n, 0);
6-
for (int i = 2; i < n; ++i) {
7-
if (nums[i] + nums[i - 2] == (nums[i - 1] * 2)) {
8-
dp[i] = 1 + dp[i - 1];
4+
int ans = 0, cnt = 0;
5+
int d = 3000;
6+
for (int i = 0; i < nums.size() - 1; ++i) {
7+
if (nums[i + 1] - nums[i] == d) {
8+
++cnt;
9+
} else {
10+
d = nums[i + 1] - nums[i];
11+
cnt = 0;
912
}
13+
ans += cnt;
1014
}
11-
int res = 0;
12-
for (auto e : dp) {
13-
res += e;
14-
}
15-
return res;
15+
return ans;
1616
}
1717
};
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
func numberOfArithmeticSlices(nums []int) int {
2-
n := len(nums)
3-
dp := make([]int, n)
4-
for i := 2; i < n; i++ {
5-
if nums[i]-nums[i-1] == nums[i-1]-nums[i-2] {
6-
dp[i] = 1 + dp[i-1]
1+
func numberOfArithmeticSlices(nums []int) (ans int) {
2+
cnt, d := 0, 3000
3+
for i, b := range nums[1:] {
4+
a := nums[i]
5+
if b-a == d {
6+
cnt++
7+
} else {
8+
d = b - a
9+
cnt = 0
710
}
11+
ans += cnt
812
}
9-
res := 0
10-
for _, e := range dp {
11-
res += e
12-
}
13-
return res
13+
return
1414
}

0 commit comments

Comments
 (0)