forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode448.java
More file actions
39 lines (34 loc) · 938 Bytes
/
Leetcode448.java
File metadata and controls
39 lines (34 loc) · 938 Bytes
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
package com.company;
import java.util.ArrayList;
import java.util.List;
public class Leetcode448 {
public static void main(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);
}
static List<Integer> disappear(int[] arr) {
int i = 0;
while (i < arr.length) {
int correctIndex = arr[i] - 1;
if (arr[i] != arr[correctIndex]) {
swap(arr, i, correctIndex);
} else {
i++;
}
}
List<Integer> ans = new ArrayList<>();
for (int j = 0; j < arr.length; j++) {
if (arr[j] != j + 1)
{ ans.add(j+1);
}
}
return ans;
}
static void swap(int[] arr, int a, int b){
int temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
}