-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLc_1389.java
More file actions
68 lines (46 loc) · 1.45 KB
/
Lc_1389.java
File metadata and controls
68 lines (46 loc) · 1.45 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// LC- 1389. Create Target Array in the Given Order
import java.util.ArrayList;
import java.util.List;
public class Lc_1389 {
public static void main(String[] args) {
int[] nums = new int[]{0,1,2,3,4};
int[] index = new int[]{0,1,2,2,1};
// using .add() method in list
int[] res1 = method1(nums, index);
System.out.println("using .add() method in list");
for(int n : res1){
System.out.print(n + ", ");
}
System.out.println(" ");
// using swaping technique
int[] res2 = method2(nums, index);
System.out.println("using swaping technique");
for(int n : res2){
System.out.print(n + ", ");
}
}
// using swaping technique
static int[] method2(int[] nums, int[] index) {
int[] temp = new int[nums.length];
for(int i = 0; i < nums.length; i++){
for(int j = i; j > index[i]; j-- ){
temp[j] = temp[j-1];
}
temp[index[i]] = nums[i];
}
return temp;
}
// using .add() method in list
static int[] method1(int[] nums, int[] index) {
List<Integer> target = new ArrayList<>();
for(int i=0;i<nums.length;i++)
{
target.add(index[i],nums[i]);
}
int[] arr = new int[target.size()];
for (int i = 0; i < target.size(); i++) {
arr[i] = target.get(i);
}
return arr;
}
}