40
40
41
41
<!-- 这里可写通用的实现逻辑 -->
42
42
43
- - 遍历输入数组的每个元素一次。
44
- - 把 ` abs(nums[i]) - 1 ` 索引位置的元素标记为负数。即 ` nums[abs(nums[i]) - 1] *= -1 ` 。
45
- - 然后遍历数组,若当前数组元素 ` nums[i] ` 为负数,说明我们在数组中存在数字 ` i+1 ` 。否则,说明数组不存在数字 ` i+1 ` ,添加到结果列表中。
43
+ ** 方法一:数组或哈希表**
44
+
45
+ 我们可以使用数组或哈希表记录数组中的数字,然后遍历 ` [1, n] ` 区间内的数字,若数字不存在于数组或哈希表中,则说明数组中缺失该数字,将其添加到结果列表中。
46
+
47
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组长度。
48
+
49
+ ** 方法二:原地修改**
50
+
51
+ 我们可以遍历数组 $nums$,将 $|nums[ i] |-1$ 位置的数字标记为负数,表示数组 $nums[ i] $ 出现过。最后遍历数组 $nums$,若 $nums[ i] $ 为正数,则说明数组中缺失 $i+1$,将其添加到结果列表中。
52
+
53
+ 遍历结束后,返回结果列表即可。
54
+
55
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。
46
56
47
57
<!-- tabs:start -->
48
58
53
63
``` python
54
64
class Solution :
55
65
def findDisappearedNumbers (self , nums : List[int ]) -> List[int ]:
56
- for num in nums:
57
- idx = abs (num) - 1
58
- if nums[idx] > 0 :
59
- nums[idx] *= - 1
60
- return [i + 1 for i, v in enumerate (nums) if v > 0 ]
66
+ s = set (nums)
67
+ return [x for x in range (1 , len (nums) + 1 ) if x not in s]
68
+ ```
69
+
70
+ ``` python
71
+ class Solution :
72
+ def findDisappearedNumbers (self , nums : List[int ]) -> List[int ]:
73
+ for x in nums:
74
+ i = abs (x) - 1
75
+ if nums[i] > 0 :
76
+ nums[i] *= - 1
77
+ return [i + 1 for i in range (len (nums)) if nums[i] > 0 ]
61
78
```
62
79
63
80
### ** Java**
@@ -68,40 +85,39 @@ class Solution:
68
85
class Solution {
69
86
public List<Integer > findDisappearedNumbers (int [] nums ) {
70
87
int n = nums. length;
71
- for (int i = 0 ; i < n; ++ i) {
72
- int idx = Math . abs(nums[i]) - 1 ;
73
- if (nums[idx] > 0 ) {
74
- nums[idx] *= - 1 ;
75
- }
88
+ boolean [] s = new boolean [n + 1 ];
89
+ for (int x : nums) {
90
+ s[x] = true ;
76
91
}
77
- List<Integer > res = new ArrayList<> ();
78
- for (int i = 0 ; i < n; ++ i ) {
79
- if (nums [i] > 0 ) {
80
- res . add(i + 1 );
92
+ List<Integer > ans = new ArrayList<> ();
93
+ for (int i = 1 ; i <= n; i ++ ) {
94
+ if (! s [i]) {
95
+ ans . add(i);
81
96
}
82
97
}
83
- return res ;
98
+ return ans ;
84
99
}
85
100
}
86
101
```
87
102
88
- ### ** TypeScript**
89
-
90
- ``` ts
91
- function findDisappearedNumbers(nums : number []): number [] {
92
- for (let i = 0 ; i < nums .length ; i ++ ) {
93
- let idx = Math .abs (nums [i ]) - 1 ;
94
- if (nums [idx ] > 0 ) {
95
- nums [idx ] *= - 1 ;
103
+ ``` java
104
+ class Solution {
105
+ public List<Integer > findDisappearedNumbers (int [] nums ) {
106
+ int n = nums. length;
107
+ for (int x : nums) {
108
+ int i = Math . abs(x) - 1 ;
109
+ if (nums[i] > 0 ) {
110
+ nums[i] *= - 1 ;
111
+ }
96
112
}
97
- }
98
- let ans = [];
99
- for ( let i = 0 ; i < nums . length ; i ++ ) {
100
- if ( nums [ i ] > 0 ) {
101
- ans . push ( i + 1 );
113
+ List< Integer > ans = new ArrayList<> ();
114
+ for ( int i = 0 ; i < n; i ++ ) {
115
+ if (nums[i] > 0 ) {
116
+ ans . add(i + 1 );
117
+ }
102
118
}
119
+ return ans;
103
120
}
104
- return ans ;
105
121
}
106
122
```
107
123
@@ -112,45 +128,122 @@ class Solution {
112
128
public:
113
129
vector<int > findDisappearedNumbers(vector<int >& nums) {
114
130
int n = nums.size();
115
- for (int i = 0; i < n; ++i) {
116
- int idx = abs(nums [ i ] ) - 1 ;
117
- if ( nums[ idx ] > 0)
118
- nums [ idx ] * = -1 ;
131
+ bool s [ n + 1 ] ;
132
+ memset(s, false, sizeof(s)) ;
133
+ for (int& x : nums) {
134
+ s [ x ] = true ;
119
135
}
120
- vector<int > res;
136
+ vector<int > ans;
137
+ for (int i = 1; i <= n; ++i) {
138
+ if (!s[ i] ) {
139
+ ans.push_back(i);
140
+ }
141
+ }
142
+ return ans;
143
+ }
144
+ };
145
+ ```
146
+
147
+ ```cpp
148
+ class Solution {
149
+ public:
150
+ vector<int> findDisappearedNumbers(vector<int>& nums) {
151
+ int n = nums.size();
152
+ for (int& x : nums) {
153
+ int i = abs(x) - 1;
154
+ if (nums[i] > 0) {
155
+ nums[i] = -nums[i];
156
+ }
157
+ }
158
+ vector<int> ans;
121
159
for (int i = 0; i < n; ++i) {
122
- if (nums[ i] > 0)
123
- res.push_back(i + 1);
160
+ if (nums[i] > 0) {
161
+ ans.push_back(i + 1);
162
+ }
124
163
}
125
- return res ;
164
+ return ans ;
126
165
}
127
166
};
128
167
```
129
168
130
169
### ** Go**
131
170
132
171
``` go
133
- func findDisappearedNumbers(nums []int) []int {
134
- for _, num := range nums {
135
- idx := abs(num) - 1
136
- if nums[idx] > 0 {
137
- nums[idx] *= -1
172
+ func findDisappearedNumbers (nums []int ) (ans []int ) {
173
+ n := len (nums)
174
+ s := make ([]bool , n+1 )
175
+ for _ , x := range nums {
176
+ s[x] = true
177
+ }
178
+ for i := 1 ; i <= n; i++ {
179
+ if !s[i] {
180
+ ans = append (ans, i)
181
+ }
182
+ }
183
+ return
184
+ }
185
+ ```
186
+
187
+ ``` go
188
+ func findDisappearedNumbers (nums []int ) (ans []int ) {
189
+ n := len (nums)
190
+ for _ , x := range nums {
191
+ i := abs (x) - 1
192
+ if nums[i] > 0 {
193
+ nums[i] = -nums[i]
138
194
}
139
195
}
140
- var res []int
141
- for i, num := range nums {
142
- if num > 0 {
143
- res = append(res, i+1)
196
+ for i := 0 ; i < n; i++ {
197
+ if nums[i] > 0 {
198
+ ans = append (ans, i+1 )
144
199
}
145
200
}
146
- return res
201
+ return
147
202
}
148
203
149
- func abs(a int) int {
150
- if a > 0 {
151
- return a
204
+ func abs (x int ) int {
205
+ if x < 0 {
206
+ return -x
152
207
}
153
- return -a
208
+ return x
209
+ }
210
+ ```
211
+
212
+ ### ** TypeScript**
213
+
214
+ ``` ts
215
+ function findDisappearedNumbers(nums : number []): number [] {
216
+ const n = nums .length ;
217
+ const s: boolean [] = new Array (n + 1 ).fill (false );
218
+ for (const x of nums ) {
219
+ s [x ] = true ;
220
+ }
221
+ const ans: number [] = [];
222
+ for (let i = 1 ; i <= n ; ++ i ) {
223
+ if (! s [i ]) {
224
+ ans .push (i );
225
+ }
226
+ }
227
+ return ans ;
228
+ }
229
+ ```
230
+
231
+ ``` ts
232
+ function findDisappearedNumbers(nums : number []): number [] {
233
+ const n = nums .length ;
234
+ for (const x of nums ) {
235
+ const i = Math .abs (x ) - 1 ;
236
+ if (nums [i ] > 0 ) {
237
+ nums [i ] *= - 1 ;
238
+ }
239
+ }
240
+ const ans: number [] = [];
241
+ for (let i = 0 ; i < n ; ++ i ) {
242
+ if (nums [i ] > 0 ) {
243
+ ans .push (i + 1 );
244
+ }
245
+ }
246
+ return ans ;
154
247
}
155
248
```
156
249
0 commit comments