-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild an array with stack.java
More file actions
46 lines (41 loc) · 945 Bytes
/
Build an array with stack.java
File metadata and controls
46 lines (41 loc) · 945 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
40
41
42
43
44
45
//using stack
class Solution {
public List<String> buildArray(int[] target, int n) {
Stack<String> stack= new Stack<>();
int i=1;
int pos=0;
while(pos<target.length)
{
stack.push("Push");
if(i!=target[pos])
stack.push("Pop");
else
pos++;
i++;
}
return stack;
}
}
//using arraylist
class Solution {
public List<String> buildArray(int[] target, int n) {
List<String> list=new ArrayList<>();
int c=0;
int r=0;
for(int i=1;i<=n;i++){
if(c==target.length){
break;
}
else if(target[r] == i){
list.add("Push");
c++;
r++;
}
else{
list.add("Push");
list.add("Pop");
}
}
return list;
}
}