Skip to content

Commit 8da6c8a

Browse files
committed
solve 448.find-all-numbers-disappeared-in-an-array
1 parent eeff427 commit 8da6c8a

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* @lc app=leetcode id=448 lang=java
3+
*
4+
* [448] Find All Numbers Disappeared in an Array
5+
*
6+
* https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/
7+
*
8+
* algorithms
9+
* Easy (54.04%)
10+
* Likes: 1842
11+
* Dislikes: 170
12+
* Total Accepted: 174.7K
13+
* Total Submissions: 322.9K
14+
* Testcase Example: '[4,3,2,7,8,2,3,1]'
15+
*
16+
* Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some
17+
* elements appear twice and others appear once.
18+
*
19+
* Find all the elements of [1, n] inclusive that do not appear in this array.
20+
*
21+
* Could you do it without extra space and in O(n) runtime? You may assume the
22+
* returned list does not count as extra space.
23+
*
24+
* Example:
25+
*
26+
* Input:
27+
* [4,3,2,7,8,2,3,1]
28+
*
29+
* Output:
30+
* [5,6]
31+
*
32+
*
33+
*/
34+
class Solution {
35+
public List<Integer> findDisappearedNumbers(int[] nums) {
36+
List<Integer> list = new ArrayList<>();
37+
for (int i = 0; i < nums.length; i++) {
38+
int index = Math.abs(nums[i]) - 1;
39+
if (nums[index] > 0) {
40+
nums[index] = -nums[index];
41+
}
42+
}
43+
44+
for (int i = 0; i < nums.length; i++) {
45+
if (nums[i] > 0) {
46+
list.add(i + 1);
47+
}
48+
}
49+
50+
return list;
51+
}
52+
}
53+

0 commit comments

Comments
 (0)