Skip to content

Commit 02c5c2b

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0448
1 parent 863e768 commit 02c5c2b

File tree

4 files changed

+84
-12
lines changed

4 files changed

+84
-12
lines changed

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

+33-2
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,53 @@
2525

2626
<!-- 这里可写通用的实现逻辑 -->
2727

28+
- 遍历输入数组的每个元素一次。
29+
-`|nums[i]|-1` 索引位置的元素标记为负数。即 `nums[|nums[i]|-1]` \* -1。
30+
- 然后遍历数组,若当前数组元素 `nums[i]` 为负数,说明我们在数组中存在数字 `i+1`。否则,说明数组不存在数字 `i+1`,添加到结果列表中。
31+
2832
<!-- tabs:start -->
2933

3034
### **Python3**
3135

3236
<!-- 这里可写当前语言的特殊实现逻辑 -->
3337

3438
```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
3650
```
3751

3852
### **Java**
3953

4054
<!-- 这里可写当前语言的特殊实现逻辑 -->
4155

4256
```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+
}
4475
```
4576

4677
### **...**

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

+29-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,40 @@
3535
### **Python3**
3636

3737
```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
3949
```
4050

4151
### **Java**
4252

4353
```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+
}
4572
```
4673

4774
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
class Solution {
22
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+
}
79
}
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);
1114
}
1215
}
13-
return result;
16+
return res;
1417
}
1518
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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

0 commit comments

Comments
 (0)