-
Notifications
You must be signed in to change notification settings - Fork 0
/
Design_an_Ordered_Stream_1656.java
50 lines (40 loc) · 1.26 KB
/
Design_an_Ordered_Stream_1656.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
40
41
42
43
44
45
46
47
48
49
50
package leetcode;
import java.util.ArrayList;
import java.util.List;
class OrderedStream {
private int size;
private String[] values;
private int ptr = 0;
public OrderedStream(int n) {
this.size = n;
values = new String[this.size];
}
public List<String> insert(int idKey, String value) {
List<String> stream = new ArrayList<>();
values[idKey - 1] = value;
while (ptr < size) {
if (values[ptr] == null) {
return stream;
} else {
stream.add(values[ptr]);
ptr++;
}
}
return stream;
}
}
/**
* Your OrderedStream object will be instantiated and called as such:
* OrderedStream obj = new OrderedStream(n); List<String> param_1 =
* obj.insert(idKey,value);
*/
public class Design_an_Ordered_Stream_1656 {
public static void main(String[] args) {
OrderedStream os = new OrderedStream(5);
System.out.println(os.insert(3, "ccccc")); // Inserts (3, "ccccc"), returns [].
System.out.println(os.insert(1, "aaaaa")); // Inserts (1, "aaaaa"), returns ["aaaaa"].
System.out.println(os.insert(2, "bbbbb")); // Inserts (2, "bbbbb"), returns ["bbbbb", "ccccc"].
System.out.println(os.insert(5, "eeeee")); // Inserts (5, "eeeee"), returns [].
System.out.println(os.insert(4, "ddddd")); // Inserts (4, "ddddd"), returns ["ddddd", "eeeee"].
}
}