File tree 6 files changed +159
-52
lines changed
solution/0400-0499/0448.Find All Numbers Disappeared in an Array
6 files changed +159
-52
lines changed Original file line number Diff line number Diff line change 28
28
<!-- 这里可写通用的实现逻辑 -->
29
29
30
30
- 遍历输入数组的每个元素一次。
31
- - 把 ` | nums[i]|- 1` 索引位置的元素标记为负数。即 ` nums[| nums[i]|-1] ` \* -1。
31
+ - 把 ` abs( nums[i]) - 1` 索引位置的元素标记为负数。即 ` nums[abs( nums[i]) - 1] *= -1 ` 。
32
32
- 然后遍历数组,若当前数组元素 ` nums[i] ` 为负数,说明我们在数组中存在数字 ` i+1 ` 。否则,说明数组不存在数字 ` i+1 ` ,添加到结果列表中。
33
33
34
34
<!-- tabs:start -->
41
41
class Solution :
42
42
def findDisappearedNumbers (self , nums : List[int ]) -> List[int ]:
43
43
for num in nums:
44
- index = abs (num) - 1
45
- if nums[index] > 0 :
46
- nums[index] *= - 1
47
- res = []
48
- for i, v in enumerate (nums):
49
- if v > 0 :
50
- res.append(i + 1 )
51
- return res
44
+ idx = abs (num) - 1
45
+ if nums[idx] > 0 :
46
+ nums[idx] *= - 1
47
+ return [i + 1 for i, v in enumerate (nums) if v > 0 ]
52
48
```
53
49
54
50
### ** Java**
@@ -60,9 +56,9 @@ class Solution {
60
56
public List<Integer > findDisappearedNumbers (int [] nums ) {
61
57
int n = nums. length;
62
58
for (int i = 0 ; i < n; ++ i) {
63
- int index = Math . abs(nums[i]) - 1 ;
64
- if (nums[index ] > 0 ) {
65
- nums[index ] *= - 1 ;
59
+ int idx = Math . abs(nums[i]) - 1 ;
60
+ if (nums[idx ] > 0 ) {
61
+ nums[idx ] *= - 1 ;
66
62
}
67
63
}
68
64
List<Integer > res = new ArrayList<> ();
@@ -76,6 +72,57 @@ class Solution {
76
72
}
77
73
```
78
74
75
+ ### ** C++**
76
+
77
+ ``` cpp
78
+ class Solution {
79
+ public:
80
+ vector<int > findDisappearedNumbers(vector<int > &nums) {
81
+ int n = nums.size();
82
+ for (int i = 0; i < n; ++i)
83
+ {
84
+ int idx = abs(nums[ i] ) - 1;
85
+ if (nums[ idx] > 0)
86
+ nums[ idx] * = -1;
87
+ }
88
+ vector<int > res;
89
+ for (int i = 0; i < n; ++i)
90
+ {
91
+ if (nums[ i] > 0)
92
+ res.push_back(i + 1);
93
+ }
94
+ return res;
95
+ }
96
+ };
97
+ ```
98
+
99
+ ### **Go**
100
+
101
+ ```go
102
+ func findDisappearedNumbers(nums []int) []int {
103
+ for _, num := range nums {
104
+ idx := abs(num) - 1
105
+ if nums[idx] > 0 {
106
+ nums[idx] *= -1
107
+ }
108
+ }
109
+ var res []int
110
+ for i, num := range nums {
111
+ if num > 0 {
112
+ res = append(res, i+1)
113
+ }
114
+ }
115
+ return res
116
+ }
117
+
118
+ func abs(a int) int {
119
+ if a > 0 {
120
+ return a
121
+ }
122
+ return -a
123
+ }
124
+ ```
125
+
79
126
### ** ...**
80
127
81
128
```
Original file line number Diff line number Diff line change 37
37
class Solution :
38
38
def findDisappearedNumbers (self , nums : List[int ]) -> List[int ]:
39
39
for num in nums:
40
- index = abs (num) - 1
41
- if nums[index] > 0 :
42
- nums[index] *= - 1
43
- res = []
44
- for i, v in enumerate (nums):
45
- if v > 0 :
46
- res.append(i + 1 )
47
- return res
40
+ idx = abs (num) - 1
41
+ if nums[idx] > 0 :
42
+ nums[idx] *= - 1
43
+ return [i + 1 for i, v in enumerate (nums) if v > 0 ]
48
44
```
49
45
50
46
### ** Java**
@@ -54,9 +50,9 @@ class Solution {
54
50
public List<Integer > findDisappearedNumbers (int [] nums ) {
55
51
int n = nums. length;
56
52
for (int i = 0 ; i < n; ++ i) {
57
- int index = Math . abs(nums[i]) - 1 ;
58
- if (nums[index ] > 0 ) {
59
- nums[index ] *= - 1 ;
53
+ int idx = Math . abs(nums[i]) - 1 ;
54
+ if (nums[idx ] > 0 ) {
55
+ nums[idx ] *= - 1 ;
60
56
}
61
57
}
62
58
List<Integer > res = new ArrayList<> ();
@@ -70,6 +66,57 @@ class Solution {
70
66
}
71
67
```
72
68
69
+ ### ** C++**
70
+
71
+ ``` cpp
72
+ class Solution {
73
+ public:
74
+ vector<int > findDisappearedNumbers(vector<int > &nums) {
75
+ int n = nums.size();
76
+ for (int i = 0; i < n; ++i)
77
+ {
78
+ int idx = abs(nums[ i] ) - 1;
79
+ if (nums[ idx] > 0)
80
+ nums[ idx] * = -1;
81
+ }
82
+ vector<int > res;
83
+ for (int i = 0; i < n; ++i)
84
+ {
85
+ if (nums[ i] > 0)
86
+ res.push_back(i + 1);
87
+ }
88
+ return res;
89
+ }
90
+ };
91
+ ```
92
+
93
+ ### **Go**
94
+
95
+ ```go
96
+ func findDisappearedNumbers(nums []int) []int {
97
+ for _, num := range nums {
98
+ idx := abs(num) - 1
99
+ if nums[idx] > 0 {
100
+ nums[idx] *= -1
101
+ }
102
+ }
103
+ var res []int
104
+ for i, num := range nums {
105
+ if num > 0 {
106
+ res = append(res, i+1)
107
+ }
108
+ }
109
+ return res
110
+ }
111
+
112
+ func abs(a int) int {
113
+ if a > 0 {
114
+ return a
115
+ }
116
+ return -a
117
+ }
118
+ ```
119
+
73
120
### ** ...**
74
121
75
122
```
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
- vector<int > findDisappearedNumbers (vector<int >& nums) {
4
- int len = nums.size ();
5
- vector<int > ans;
6
- if (len == 0 )return ans;
7
-
8
- int index ;
9
- for (int i = 0 ;i<len;++i){
10
- index = abs (nums[i]) - 1 ;
11
-
12
- if (nums[index ] > 0 )
13
- nums[index ] = -nums[index ];
3
+ vector<int > findDisappearedNumbers (vector<int > &nums) {
4
+ int n = nums.size ();
5
+ for (int i = 0 ; i < n; ++i)
6
+ {
7
+ int idx = abs (nums[i]) - 1 ;
8
+ if (nums[idx] > 0 )
9
+ nums[idx] *= -1 ;
14
10
}
15
-
16
-
17
- for ( int i = 0 ;i<len;++i) {
18
- if (nums[i] > 0 )
19
- ans .push_back (i+ 1 );
11
+ vector< int > res;
12
+ for ( int i = 0 ; i < n; ++i)
13
+ {
14
+ if (nums[i] > 0 )
15
+ res .push_back (i + 1 );
20
16
}
21
-
22
- return ans;
17
+ return res;
23
18
}
24
19
};
Original file line number Diff line number Diff line change
1
+ func findDisappearedNumbers (nums []int ) []int {
2
+ for _ , num := range nums {
3
+ idx := abs (num ) - 1
4
+ if nums [idx ] > 0 {
5
+ nums [idx ] *= - 1
6
+ }
7
+ }
8
+ var res []int
9
+ for i , num := range nums {
10
+ if num > 0 {
11
+ res = append (res , i + 1 )
12
+ }
13
+ }
14
+ return res
15
+ }
16
+
17
+ func abs (a int ) int {
18
+ if a > 0 {
19
+ return a
20
+ }
21
+ return - a
22
+ }
Original file line number Diff line number Diff line change @@ -2,9 +2,9 @@ class Solution {
2
2
public List <Integer > findDisappearedNumbers (int [] nums ) {
3
3
int n = nums .length ;
4
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 ;
5
+ int idx = Math .abs (nums [i ]) - 1 ;
6
+ if (nums [idx ] > 0 ) {
7
+ nums [idx ] *= -1 ;
8
8
}
9
9
}
10
10
List <Integer > res = new ArrayList <>();
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def findDisappearedNumbers (self , nums : List [int ]) -> List [int ]:
3
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
4
+ idx = abs (num ) - 1
5
+ if nums [idx ] > 0 :
6
+ nums [idx ] *= - 1
7
+ return [i + 1 for i , v in enumerate (nums ) if v > 0 ]
You can’t perform that action at this time.
0 commit comments