- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathLeetcode448.java
39 lines (34 loc) · 938 Bytes
/
Leetcode448.java
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
packagecom.company;
importjava.util.ArrayList;
importjava.util.List;
publicclassLeetcode448 {
publicstaticvoidmain(String[] args) {
int[] arr={4,3,2,7,8,2,3,1};
// List<Integer> ans=new ArrayList<>();
List<Integer> ans=disappear(arr);
System.out.println(ans);
}
staticList<Integer> disappear(int[] arr) {
inti = 0;
while (i < arr.length) {
intcorrectIndex = arr[i] - 1;
if (arr[i] != arr[correctIndex]) {
swap(arr, i, correctIndex);
} else {
i++;
}
}
List<Integer> ans = newArrayList<>();
for (intj = 0; j < arr.length; j++) {
if (arr[j] != j + 1)
{ ans.add(j+1);
}
}
returnans;
}
staticvoidswap(int[] arr, inta, intb){
inttemp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
}