Skip to content

Commit cddc493

Browse files
committed
feat: add solutions to lc problem: No.1228
No.1228.Missing Number In Arithmetic Progression
1 parent d6a188f commit cddc493

File tree

6 files changed

+92
-23
lines changed

6 files changed

+92
-23
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,28 @@
4343

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

46+
**方法一:等差数列求和公式**
47+
48+
等差数列求和公式为 $\frac{n(a_1 + a_n)}{2}$,其中 $n$ 为等差数列的项数,$a_1$ 为等差数列的首项,$a_n$ 为等差数列的末项。
49+
50+
因为题目中给出的数组是一个等差数列,且缺失了一个数,所以数组的项数为 $n + 1$,首项为 $a_1$,末项为 $a_n$,则数组的和为 $\frac{n + 1}{2}(a_1 + a_n)$。
51+
52+
因此,缺失的数为 $\frac{n + 1}{2}(a_1 + a_n) - \sum_{i = 0}^n a_i$。
53+
54+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。
55+
4656
<!-- tabs:start -->
4757

4858
### **Python3**
4959

5060
<!-- 这里可写当前语言的特殊实现逻辑 -->
5161

62+
```python
63+
class Solution:
64+
def missingNumber(self, arr: List[int]) -> int:
65+
return (arr[0] + arr[-1]) * (len(arr) + 1) // 2 - sum(arr)
66+
```
67+
5268
```python
5369
class Solution:
5470
def missingNumber(self, arr: List[int]) -> int:
@@ -64,6 +80,17 @@ class Solution:
6480

6581
<!-- 这里可写当前语言的特殊实现逻辑 -->
6682

83+
```java
84+
class Solution {
85+
public int missingNumber(int[] arr) {
86+
int n = arr.length;
87+
int x = (arr[0] + arr[n - 1]) * (n + 1) / 2;
88+
int y = Arrays.stream(arr).sum();
89+
return x - y;
90+
}
91+
}
92+
```
93+
6794
```java
6895
class Solution {
6996
public int missingNumber(int[] arr) {
@@ -81,6 +108,18 @@ class Solution {
81108

82109
### **C++**
83110

111+
```cpp
112+
class Solution {
113+
public:
114+
int missingNumber(vector<int>& arr) {
115+
int n = arr.size();
116+
int x = (arr[0] + arr[n - 1]) * (n + 1) / 2;
117+
int y = accumulate(arr.begin(), arr.end(), 0);
118+
return x - y;
119+
}
120+
};
121+
```
122+
84123
```cpp
85124
class Solution {
86125
public:

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141

4242
### **Python3**
4343

44+
```python
45+
class Solution:
46+
def missingNumber(self, arr: List[int]) -> int:
47+
return (arr[0] + arr[-1]) * (len(arr) + 1) // 2 - sum(arr)
48+
```
49+
4450
```python
4551
class Solution:
4652
def missingNumber(self, arr: List[int]) -> int:
@@ -54,6 +60,17 @@ class Solution:
5460

5561
### **Java**
5662

63+
```java
64+
class Solution {
65+
public int missingNumber(int[] arr) {
66+
int n = arr.length;
67+
int x = (arr[0] + arr[n - 1]) * (n + 1) / 2;
68+
int y = Arrays.stream(arr).sum();
69+
return x - y;
70+
}
71+
}
72+
```
73+
5774
```java
5875
class Solution {
5976
public int missingNumber(int[] arr) {
@@ -71,6 +88,18 @@ class Solution {
7188

7289
### **C++**
7390

91+
```cpp
92+
class Solution {
93+
public:
94+
int missingNumber(vector<int>& arr) {
95+
int n = arr.size();
96+
int x = (arr[0] + arr[n - 1]) * (n + 1) / 2;
97+
int y = accumulate(arr.begin(), arr.end(), 0);
98+
return x - y;
99+
}
100+
};
101+
```
102+
74103
```cpp
75104
class Solution {
76105
public:
@@ -86,6 +115,18 @@ public:
86115

87116
### **Go**
88117

118+
```go
119+
func missingNumber(arr []int) int {
120+
n := len(arr)
121+
x := (arr[0] + arr[n-1]) * (n + 1) / 2
122+
y := 0
123+
for _, v := range arr {
124+
y += v
125+
}
126+
return x - y
127+
}
128+
```
129+
89130
```go
90131
func missingNumber(arr []int) int {
91132
n := len(arr)

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ class Solution {
22
public:
33
int missingNumber(vector<int>& arr) {
44
int n = arr.size();
5-
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;
8-
return arr[0];
5+
int x = (arr[0] + arr[n - 1]) * (n + 1) / 2;
6+
int y = accumulate(arr.begin(), arr.end(), 0);
7+
return x - y;
98
}
109
};
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
func missingNumber(arr []int) int {
22
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-
}
3+
x := (arr[0] + arr[n-1]) * (n + 1) / 2
4+
y := 0
5+
for _, v := range arr {
6+
y += v
87
}
9-
return arr[0]
8+
return x - y
109
}
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
class Solution {
22
public int missingNumber(int[] arr) {
33
int n = arr.length;
4-
int d = (arr[n - 1] - arr[0]) / n;
5-
for (int i = 1; i < n; ++i) {
6-
if (arr[i] != arr[i - 1] + d) {
7-
return arr[i - 1] + d;
8-
}
9-
}
10-
return arr[0];
4+
int x = (arr[0] + arr[n - 1]) * (n + 1) / 2;
5+
int y = Arrays.stream(arr).sum();
6+
return x - y;
117
}
128
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
class Solution:
22
def missingNumber(self, arr: List[int]) -> int:
3-
n = len(arr)
4-
d = (arr[-1] - arr[0]) // n
5-
for i in range(1, n):
6-
if arr[i] != arr[i - 1] + d:
7-
return arr[i - 1] + d
8-
return arr[0]
3+
return (arr[0] + arr[-1]) * (len(arr) + 1) // 2 - sum(arr)

0 commit comments

Comments
 (0)