-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindDisappearedNumbers.java
More file actions
41 lines (33 loc) · 1.02 KB
/
findDisappearedNumbers.java
File metadata and controls
41 lines (33 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.*;
public class findDisappearedNumbers{
public static void main(String[] args){
int nums[]={4,3,2,7,8,2,3,1};
List<Integer> list = find_numbersB(nums);
System.out.println(list);
}
public static List<Integer> find_numbers(int nums[]){
Set<Integer> nums_set = new HashSet<Integer>();
Set<Integer> all_nums = new HashSet<Integer>();
List<Integer> list= new ArrayList<>();
for(int x:nums) nums_set.add(x);
for(int i=1;i<nums.length+1;i++) all_nums.add(i);
all_nums.removeAll(nums_set);
for(int n:all_nums) list.add(n);
return list;
}
public static List<Integer> find_numbersB(int nums[]){
List<Integer> ret = new ArrayList<Integer>();
for(int i = 0; i < nums.length; i++) {
int val = Math.abs(nums[i]) - 1;
if(nums[val] > 0) {
nums[val] = -nums[val];
}
}
for(int i = 0; i < nums.length; i++) {
if(nums[i] > 0) {
ret.add(i+1);
}
}
return ret;
}
}