Skip to content

Commit 3810f9e

Browse files
committed
feat: add solutions to lc problem: No.0448.Find All Members Disappeared
in an Array
1 parent 1ca39ab commit 3810f9e

File tree

6 files changed

+159
-52
lines changed

6 files changed

+159
-52
lines changed

solution/0400-0499/0448.Find All Numbers Disappeared in an Array/README.md

+59-12
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<!-- 这里可写通用的实现逻辑 -->
2929

3030
- 遍历输入数组的每个元素一次。
31-
-`|nums[i]|-1` 索引位置的元素标记为负数。即 `nums[|nums[i]|-1]` \* -1。
31+
-`abs(nums[i]) - 1` 索引位置的元素标记为负数。即 `nums[abs(nums[i]) - 1] *= -1`
3232
- 然后遍历数组,若当前数组元素 `nums[i]` 为负数,说明我们在数组中存在数字 `i+1`。否则,说明数组不存在数字 `i+1`,添加到结果列表中。
3333

3434
<!-- tabs:start -->
@@ -41,14 +41,10 @@
4141
class Solution:
4242
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
4343
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]
5248
```
5349

5450
### **Java**
@@ -60,9 +56,9 @@ class Solution {
6056
public List<Integer> findDisappearedNumbers(int[] nums) {
6157
int n = nums.length;
6258
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;
6662
}
6763
}
6864
List<Integer> res = new ArrayList<>();
@@ -76,6 +72,57 @@ class Solution {
7672
}
7773
```
7874

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+
79126
### **...**
80127

81128
```

solution/0400-0499/0448.Find All Numbers Disappeared in an Array/README_EN.md

+58-11
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,10 @@
3737
class Solution:
3838
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
3939
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]
4844
```
4945

5046
### **Java**
@@ -54,9 +50,9 @@ class Solution {
5450
public List<Integer> findDisappearedNumbers(int[] nums) {
5551
int n = nums.length;
5652
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;
6056
}
6157
}
6258
List<Integer> res = new ArrayList<>();
@@ -70,6 +66,57 @@ class Solution {
7066
}
7167
```
7268

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+
73120
### **...**
74121

75122
```
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
class Solution {
22
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;
1410
}
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);
2016
}
21-
22-
return ans;
17+
return res;
2318
}
2419
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

solution/0400-0499/0448.Find All Numbers Disappeared in an Array/Solution.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ class Solution {
22
public List<Integer> findDisappearedNumbers(int[] nums) {
33
int n = nums.length;
44
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;
88
}
99
}
1010
List<Integer> res = new ArrayList<>();
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
class Solution:
22
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
33
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]

0 commit comments

Comments
 (0)