Skip to content

Commit fb2372e

Browse files
authored
feat: add solutions to lc problem: No.1228 (doocs#3499)
No.1228.Missing Number In Arithmetic Progression
1 parent 22044d2 commit fb2372e

File tree

7 files changed

+162
-38
lines changed

7 files changed

+162
-38
lines changed

solution/1200-1299/1228.Missing Number In Arithmetic Progression/README.md

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ tags:
6060

6161
### 方法一:等差数列求和公式
6262

63-
等差数列求和公式为 $\frac{n(a_1 + a_n)}{2}$,其中 $n$ 为等差数列的项数,$a_1$ 为等差数列的首项,$a_n$ 为等差数列的末项
63+
等差数列求和公式为 $\frac{(a_1 + a_n)n}{2}$,其中 $n$ 为等差数列的项数,等差数列的首项为 $a_1$,末项为 $a_n$。
6464

65-
因为题目中给出的数组是一个等差数列,且缺失了一个数,所以数组的项数为 $n + 1$,首项为 $a_1$,末项为 $a_n$,则数组的和为 $\frac{n + 1}{2}(a_1 + a_n)$。
65+
因为题目中给出的数组是一个等差数列,且缺失了一个数,所以数组的项数为 $n + 1$,首项为 $a_1$,末项为 $a_n$,则数组的和为 $\frac{(a_1 + a_n)(n + 1)}{2}$。
6666

67-
因此,缺失的数为 $\frac{n + 1}{2}(a_1 + a_n) - \sum_{i = 0}^n a_i$。
67+
因此,缺失的数为 $\frac{(a_1 + a_n)(n + 1)}{2} - \sum_{i = 0}^n a_i$。
6868

6969
时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。
7070

@@ -110,13 +110,22 @@ public:
110110
```go
111111
func missingNumber(arr []int) int {
112112
n := len(arr)
113-
d := (arr[n-1] - arr[0]) / n
114-
for i := 1; i < n; i++ {
115-
if arr[i] != arr[i-1]+d {
116-
return arr[i-1] + d
117-
}
113+
x := (arr[0] + arr[n-1]) * (n + 1) / 2
114+
y := 0
115+
for _, v := range arr {
116+
y += v
118117
}
119-
return arr[0]
118+
return x - y
119+
}
120+
```
121+
122+
#### TypeScript
123+
124+
```ts
125+
function missingNumber(arr: number[]): number {
126+
const x = ((arr[0] + arr.at(-1)!) * (arr.length + 1)) >> 1;
127+
const y = arr.reduce((acc, cur) => acc + cur, 0);
128+
return x - y;
120129
}
121130
```
122131

@@ -126,7 +135,15 @@ func missingNumber(arr []int) int {
126135

127136
<!-- solution:start -->
128137

129-
### 方法二
138+
### 方法二:求公差 + 遍历
139+
140+
因为题目中给出的数组是一个等差数列,且缺失了一个数,首项为 $a_1$,末项为 $a_n$,那么公差 $d = \frac{a_n - a_1}{n}$。
141+
142+
遍历数组,如果 $a_i \neq a_{i - 1} + d$,则返回 $a_{i - 1} + d$。
143+
144+
如果遍历完数组都没有找到缺失的数,说明数组的所有数都相等,直接返回数组的第一个数即可。
145+
146+
时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。
130147

131148
<!-- tabs:start -->
132149

@@ -168,13 +185,45 @@ public:
168185
int missingNumber(vector<int>& arr) {
169186
int n = arr.size();
170187
int d = (arr[n - 1] - arr[0]) / n;
171-
for (int i = 1; i < n; ++i)
172-
if (arr[i] != arr[i - 1] + d) return arr[i - 1] + d;
188+
for (int i = 1; i < n; ++i) {
189+
if (arr[i] != arr[i - 1] + d) {
190+
return arr[i - 1] + d;
191+
}
192+
}
173193
return arr[0];
174194
}
175195
};
176196
```
177197
198+
#### Go
199+
200+
```go
201+
func missingNumber(arr []int) int {
202+
n := len(arr)
203+
d := (arr[n-1] - arr[0]) / n
204+
for i := 1; i < n; i++ {
205+
if arr[i] != arr[i-1]+d {
206+
return arr[i-1] + d
207+
}
208+
}
209+
return arr[0]
210+
}
211+
```
212+
213+
#### TypeScript
214+
215+
```ts
216+
function missingNumber(arr: number[]): number {
217+
const d = ((arr.at(-1)! - arr[0]) / arr.length) | 0;
218+
for (let i = 1; i < arr.length; ++i) {
219+
if (arr[i] - arr[i - 1] !== d) {
220+
return arr[i - 1] + d;
221+
}
222+
}
223+
return arr[0];
224+
}
225+
```
226+
178227
<!-- tabs:end -->
179228

180229
<!-- solution:end -->

solution/1200-1299/1228.Missing Number In Arithmetic Progression/README_EN.md

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ tags:
5858

5959
### Solution 1: Arithmetic Series Sum Formula
6060

61-
The sum formula for an arithmetic series is $\frac{n(a_1 + a_n)}{2}$, where $n$ is the number of terms in the arithmetic series, $a_1$ is the first term of the arithmetic series, and $a_n$ is the last term of the arithmetic series.
61+
The sum formula for an arithmetic series is $\frac{(a_1 + a_n)n}{2}$, where $n$ is the number of terms in the arithmetic series, the first term is $a_1$, and the last term is $a_n$.
6262

63-
Since the array given in the problem is an arithmetic series and is missing a number, the number of terms in the array is $n + 1$, the first term is $a_1$, and the last term is $a_n$, so the sum of the array is $\frac{n + 1}{2}(a_1 + a_n)$.
63+
Since the array given in the problem is an arithmetic series with one missing number, the number of terms in the array is $n + 1$, the first term is $a_1$, and the last term is $a_n$. Therefore, the sum of the array is $\frac{(a_1 + a_n)(n + 1)}{2}$.
6464

65-
Therefore, the missing number is $\frac{n + 1}{2}(a_1 + a_n) - \sum_{i = 0}^n a_i$.
65+
Thus, the missing number is $\frac{(a_1 + a_n)(n + 1)}{2} - \sum_{i = 0}^n a_i$.
6666

67-
The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of the array.
67+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
6868

6969
<!-- tabs:start -->
7070

@@ -108,13 +108,22 @@ public:
108108
```go
109109
func missingNumber(arr []int) int {
110110
n := len(arr)
111-
d := (arr[n-1] - arr[0]) / n
112-
for i := 1; i < n; i++ {
113-
if arr[i] != arr[i-1]+d {
114-
return arr[i-1] + d
115-
}
111+
x := (arr[0] + arr[n-1]) * (n + 1) / 2
112+
y := 0
113+
for _, v := range arr {
114+
y += v
116115
}
117-
return arr[0]
116+
return x - y
117+
}
118+
```
119+
120+
#### TypeScript
121+
122+
```ts
123+
function missingNumber(arr: number[]): number {
124+
const x = ((arr[0] + arr.at(-1)!) * (arr.length + 1)) >> 1;
125+
const y = arr.reduce((acc, cur) => acc + cur, 0);
126+
return x - y;
118127
}
119128
```
120129

@@ -124,7 +133,15 @@ func missingNumber(arr []int) int {
124133

125134
<!-- solution:start -->
126135

127-
### Solution 2
136+
### Solution 2: Find Common Difference + Traverse
137+
138+
Since the array given in the problem is an arithmetic series with one missing number, the first term is $a_1$, and the last term is $a_n$. The common difference $d$ is $\frac{a_n - a_1}{n}$.
139+
140+
Traverse the array, and if $a_i \neq a_{i - 1} + d$, then return $a_{i - 1} + d$.
141+
142+
If the traversal completes without finding the missing number, it means all numbers in the array are equal. In this case, directly return the first number of the array.
143+
144+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
128145

129146
<!-- tabs:start -->
130147

@@ -166,13 +183,45 @@ public:
166183
int missingNumber(vector<int>& arr) {
167184
int n = arr.size();
168185
int d = (arr[n - 1] - arr[0]) / n;
169-
for (int i = 1; i < n; ++i)
170-
if (arr[i] != arr[i - 1] + d) return arr[i - 1] + d;
186+
for (int i = 1; i < n; ++i) {
187+
if (arr[i] != arr[i - 1] + d) {
188+
return arr[i - 1] + d;
189+
}
190+
}
171191
return arr[0];
172192
}
173193
};
174194
```
175195
196+
#### Go
197+
198+
```go
199+
func missingNumber(arr []int) int {
200+
n := len(arr)
201+
d := (arr[n-1] - arr[0]) / n
202+
for i := 1; i < n; i++ {
203+
if arr[i] != arr[i-1]+d {
204+
return arr[i-1] + d
205+
}
206+
}
207+
return arr[0]
208+
}
209+
```
210+
211+
#### TypeScript
212+
213+
```ts
214+
function missingNumber(arr: number[]): number {
215+
const d = ((arr.at(-1)! - arr[0]) / arr.length) | 0;
216+
for (let i = 1; i < arr.length; ++i) {
217+
if (arr[i] - arr[i - 1] !== d) {
218+
return arr[i - 1] + d;
219+
}
220+
}
221+
return arr[0];
222+
}
223+
```
224+
176225
<!-- tabs:end -->
177226

178227
<!-- solution:end -->
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
func missingNumber(arr []int) int {
2-
n := len(arr)
3-
d := (arr[n-1] - arr[0]) / n
4-
for i := 1; i < n; i++ {
5-
if arr[i] != arr[i-1]+d {
6-
return arr[i-1] + d
7-
}
8-
}
9-
return arr[0]
10-
}
1+
func missingNumber(arr []int) int {
2+
n := len(arr)
3+
x := (arr[0] + arr[n-1]) * (n + 1) / 2
4+
y := 0
5+
for _, v := range arr {
6+
y += v
7+
}
8+
return x - y
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function missingNumber(arr: number[]): number {
2+
const x = ((arr[0] + arr.at(-1)!) * (arr.length + 1)) >> 1;
3+
const y = arr.reduce((acc, cur) => acc + cur, 0);
4+
return x - y;
5+
}

solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution2.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ class Solution {
33
int missingNumber(vector<int>& arr) {
44
int n = arr.size();
55
int d = (arr[n - 1] - arr[0]) / n;
6-
for (int i = 1; i < n; ++i)
7-
if (arr[i] != arr[i - 1] + d) return arr[i - 1] + d;
6+
for (int i = 1; i < n; ++i) {
7+
if (arr[i] != arr[i - 1] + d) {
8+
return arr[i - 1] + d;
9+
}
10+
}
811
return arr[0];
912
}
10-
};
13+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func missingNumber(arr []int) int {
2+
n := len(arr)
3+
d := (arr[n-1] - arr[0]) / n
4+
for i := 1; i < n; i++ {
5+
if arr[i] != arr[i-1]+d {
6+
return arr[i-1] + d
7+
}
8+
}
9+
return arr[0]
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function missingNumber(arr: number[]): number {
2+
const d = ((arr.at(-1)! - arr[0]) / arr.length) | 0;
3+
for (let i = 1; i < arr.length; ++i) {
4+
if (arr[i] - arr[i - 1] !== d) {
5+
return arr[i - 1] + d;
6+
}
7+
}
8+
return arr[0];
9+
}

0 commit comments

Comments
 (0)