File tree Expand file tree Collapse file tree 6 files changed +92
-23
lines changed
solution/1200-1299/1228.Missing Number In Arithmetic Progression Expand file tree Collapse file tree 6 files changed +92
-23
lines changed Original file line number Diff line number Diff line change 43
43
44
44
<!-- 这里可写通用的实现逻辑 -->
45
45
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
+
46
56
<!-- tabs:start -->
47
57
48
58
### ** Python3**
49
59
50
60
<!-- 这里可写当前语言的特殊实现逻辑 -->
51
61
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
+
52
68
``` python
53
69
class Solution :
54
70
def missingNumber (self , arr : List[int ]) -> int :
@@ -64,6 +80,17 @@ class Solution:
64
80
65
81
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
82
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
+
67
94
``` java
68
95
class Solution {
69
96
public int missingNumber (int [] arr ) {
@@ -81,6 +108,18 @@ class Solution {
81
108
82
109
### ** C++**
83
110
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
+
84
123
```cpp
85
124
class Solution {
86
125
public:
Original file line number Diff line number Diff line change 41
41
42
42
### ** Python3**
43
43
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
+
44
50
``` python
45
51
class Solution :
46
52
def missingNumber (self , arr : List[int ]) -> int :
@@ -54,6 +60,17 @@ class Solution:
54
60
55
61
### ** Java**
56
62
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
+
57
74
``` java
58
75
class Solution {
59
76
public int missingNumber (int [] arr ) {
@@ -71,6 +88,18 @@ class Solution {
71
88
72
89
### ** C++**
73
90
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
+
74
103
```cpp
75
104
class Solution {
76
105
public:
@@ -86,6 +115,18 @@ public:
86
115
87
116
### ** Go**
88
117
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
+
89
130
``` go
90
131
func missingNumber (arr []int ) int {
91
132
n := len (arr)
Original file line number Diff line number Diff line change @@ -2,9 +2,8 @@ class Solution {
2
2
public:
3
3
int missingNumber (vector<int >& arr) {
4
4
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;
9
8
}
10
9
};
Original file line number Diff line number Diff line change 1
1
func missingNumber (arr []int ) int {
2
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
- }
3
+ x := (arr [0 ] + arr [n - 1 ]) * (n + 1 ) / 2
4
+ y := 0
5
+ for _ , v := range arr {
6
+ y += v
8
7
}
9
- return arr [ 0 ]
8
+ return x - y
10
9
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int missingNumber (int [] arr ) {
3
3
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 ;
11
7
}
12
8
}
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
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 )
You can’t perform that action at this time.
0 commit comments