File tree 4 files changed +84
-12
lines changed
solution/0400-0499/0448.Find All Numbers Disappeared in an Array
4 files changed +84
-12
lines changed Original file line number Diff line number Diff line change 25
25
26
26
<!-- 这里可写通用的实现逻辑 -->
27
27
28
+ - 遍历输入数组的每个元素一次。
29
+ - 把 ` |nums[i]|-1 ` 索引位置的元素标记为负数。即 ` nums[|nums[i]|-1] ` \* -1。
30
+ - 然后遍历数组,若当前数组元素 ` nums[i] ` 为负数,说明我们在数组中存在数字 ` i+1 ` 。否则,说明数组不存在数字 ` i+1 ` ,添加到结果列表中。
31
+
28
32
<!-- tabs:start -->
29
33
30
34
### ** Python3**
31
35
32
36
<!-- 这里可写当前语言的特殊实现逻辑 -->
33
37
34
38
``` python
35
-
39
+ class Solution :
40
+ def findDisappearedNumbers (self , nums : List[int ]) -> List[int ]:
41
+ for num in nums:
42
+ index = abs (num) - 1
43
+ if nums[index] > 0 :
44
+ nums[index] *= - 1
45
+ res = []
46
+ for i, v in enumerate (nums):
47
+ if v > 0 :
48
+ res.append(i + 1 )
49
+ return res
36
50
```
37
51
38
52
### ** Java**
39
53
40
54
<!-- 这里可写当前语言的特殊实现逻辑 -->
41
55
42
56
``` java
43
-
57
+ class Solution {
58
+ public List<Integer > findDisappearedNumbers (int [] nums ) {
59
+ int n = nums. length;
60
+ for (int i = 0 ; i < n; ++ i) {
61
+ int index = Math . abs(nums[i]) - 1 ;
62
+ if (nums[index] > 0 ) {
63
+ nums[index] *= - 1 ;
64
+ }
65
+ }
66
+ List<Integer > res = new ArrayList<> ();
67
+ for (int i = 0 ; i < n; ++ i) {
68
+ if (nums[i] > 0 ) {
69
+ res. add(i + 1 );
70
+ }
71
+ }
72
+ return res;
73
+ }
74
+ }
44
75
```
45
76
46
77
### ** ...**
Original file line number Diff line number Diff line change 35
35
### ** Python3**
36
36
37
37
``` python
38
-
38
+ class Solution :
39
+ def findDisappearedNumbers (self , nums : List[int ]) -> List[int ]:
40
+ for num in nums:
41
+ index = abs (num) - 1
42
+ if nums[index] > 0 :
43
+ nums[index] *= - 1
44
+ res = []
45
+ for i, v in enumerate (nums):
46
+ if v > 0 :
47
+ res.append(i + 1 )
48
+ return res
39
49
```
40
50
41
51
### ** Java**
42
52
43
53
``` java
44
-
54
+ class Solution {
55
+ public List<Integer > findDisappearedNumbers (int [] nums ) {
56
+ int n = nums. length;
57
+ for (int i = 0 ; i < n; ++ i) {
58
+ int index = Math . abs(nums[i]) - 1 ;
59
+ if (nums[index] > 0 ) {
60
+ nums[index] *= - 1 ;
61
+ }
62
+ }
63
+ List<Integer > res = new ArrayList<> ();
64
+ for (int i = 0 ; i < n; ++ i) {
65
+ if (nums[i] > 0 ) {
66
+ res. add(i + 1 );
67
+ }
68
+ }
69
+ return res;
70
+ }
71
+ }
45
72
```
46
73
47
74
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public List <Integer > findDisappearedNumbers (int [] nums ) {
3
- List <Integer > result = new ArrayList <>();
4
- boolean [] inx = new boolean [nums .length + 1 ];
5
- for (int num : nums ) {
6
- inx [num ] = true ;
3
+ int n = nums .length ;
4
+ for (int i = 0 ; i < n ; ++i ) {
5
+ int index = Math .abs (nums [i ]) - 1 ;
6
+ if (nums [index ] > 0 ) {
7
+ nums [index ] *= -1 ;
8
+ }
7
9
}
8
- for (int i = 1 , length = nums .length ; i <= length ; i ++) {
9
- if (!inx [i ]) {
10
- result .add (i );
10
+ List <Integer > res = new ArrayList <>();
11
+ for (int i = 0 ; i < n ; ++i ) {
12
+ if (nums [i ] > 0 ) {
13
+ res .add (i + 1 );
11
14
}
12
15
}
13
- return result ;
16
+ return res ;
14
17
}
15
18
}
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def findDisappearedNumbers (self , nums : List [int ]) -> List [int ]:
3
+ for num in nums :
4
+ index = abs (num ) - 1
5
+ if nums [index ] > 0 :
6
+ nums [index ] *= - 1
7
+ res = []
8
+ for i , v in enumerate (nums ):
9
+ if v > 0 :
10
+ res .append (i + 1 )
11
+ return res
You can’t perform that action at this time.
0 commit comments