Skip to content

Commit f470e1f

Browse files
committedAug 16, 2022
feat: add solutions to lc problem: No.1656
No.1656.Design an Ordered Stream
1 parent 29e8ff2 commit f470e1f

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed
 

‎solution/1600-1699/1656.Design an Ordered Stream/README.md

+69
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,75 @@ func (this *OrderedStream) Insert(idKey int, value string) []string {
177177
*/
178178
```
179179

180+
### **TypeScript**
181+
182+
```ts
183+
class OrderedStream {
184+
private ptr: number;
185+
private vals: string[];
186+
187+
constructor(n: number) {
188+
this.ptr = 0;
189+
this.vals = new Array(n);
190+
}
191+
192+
insert(idKey: number, value: string): string[] {
193+
this.vals[idKey - 1] = value;
194+
const res = [];
195+
while (this.vals[this.ptr] != null) {
196+
res.push(this.vals[this.ptr]);
197+
this.ptr++;
198+
}
199+
return res;
200+
}
201+
}
202+
203+
/**
204+
* Your OrderedStream object will be instantiated and called as such:
205+
* var obj = new OrderedStream(n)
206+
* var param_1 = obj.insert(idKey,value)
207+
*/
208+
```
209+
210+
### **Rust**
211+
212+
```rust
213+
struct OrderedStream {
214+
ptr: usize,
215+
vals: Vec<Option<String>>,
216+
}
217+
218+
/**
219+
* `&self` means the method takes an immutable reference.
220+
* If you need a mutable reference, change it to `&mut self` instead.
221+
*/
222+
impl OrderedStream {
223+
fn new(n: i32) -> Self {
224+
Self { ptr: 0, vals: vec![None; n as usize] }
225+
}
226+
227+
fn insert(&mut self, id_key: i32, value: String) -> Vec<String> {
228+
self.vals[(id_key - 1) as usize] = Some(value);
229+
let mut res = Vec::new();
230+
while self.ptr < self.vals.len() {
231+
if let Some(s) = &self.vals[self.ptr] {
232+
res.push(s.clone());
233+
self.ptr += 1;
234+
} else {
235+
break;
236+
}
237+
}
238+
res
239+
}
240+
}
241+
242+
/**
243+
* Your OrderedStream object will be instantiated and called as such:
244+
* let obj = OrderedStream::new(n);
245+
* let ret_1: Vec<String> = obj.insert(idKey, value);
246+
*/
247+
```
248+
180249
### **...**
181250

182251
```

‎solution/1600-1699/1656.Design an Ordered Stream/README_EN.md

+69
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,75 @@ func (this *OrderedStream) Insert(idKey int, value string) []string {
164164
*/
165165
```
166166

167+
### **TypeScript**
168+
169+
```ts
170+
class OrderedStream {
171+
private ptr: number;
172+
private vals: string[];
173+
174+
constructor(n: number) {
175+
this.ptr = 0;
176+
this.vals = new Array(n);
177+
}
178+
179+
insert(idKey: number, value: string): string[] {
180+
this.vals[idKey - 1] = value;
181+
const res = [];
182+
while (this.vals[this.ptr] != null) {
183+
res.push(this.vals[this.ptr]);
184+
this.ptr++;
185+
}
186+
return res;
187+
}
188+
}
189+
190+
/**
191+
* Your OrderedStream object will be instantiated and called as such:
192+
* var obj = new OrderedStream(n)
193+
* var param_1 = obj.insert(idKey,value)
194+
*/
195+
```
196+
197+
### **Rust**
198+
199+
```rust
200+
struct OrderedStream {
201+
ptr: usize,
202+
vals: Vec<Option<String>>,
203+
}
204+
205+
/**
206+
* `&self` means the method takes an immutable reference.
207+
* If you need a mutable reference, change it to `&mut self` instead.
208+
*/
209+
impl OrderedStream {
210+
fn new(n: i32) -> Self {
211+
Self { ptr: 0, vals: vec![None; n as usize] }
212+
}
213+
214+
fn insert(&mut self, id_key: i32, value: String) -> Vec<String> {
215+
self.vals[(id_key - 1) as usize] = Some(value);
216+
let mut res = Vec::new();
217+
while self.ptr < self.vals.len() {
218+
if let Some(s) = &self.vals[self.ptr] {
219+
res.push(s.clone());
220+
self.ptr += 1;
221+
} else {
222+
break;
223+
}
224+
}
225+
res
226+
}
227+
}
228+
229+
/**
230+
* Your OrderedStream object will be instantiated and called as such:
231+
* let obj = OrderedStream::new(n);
232+
* let ret_1: Vec<String> = obj.insert(idKey, value);
233+
*/
234+
```
235+
167236
### **...**
168237

169238
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
struct OrderedStream {
2+
ptr: usize,
3+
vals: Vec<Option<String>>,
4+
}
5+
6+
/**
7+
* `&self` means the method takes an immutable reference.
8+
* If you need a mutable reference, change it to `&mut self` instead.
9+
*/
10+
impl OrderedStream {
11+
fn new(n: i32) -> Self {
12+
Self { ptr: 0, vals: vec![None; n as usize] }
13+
}
14+
15+
fn insert(&mut self, id_key: i32, value: String) -> Vec<String> {
16+
self.vals[(id_key - 1) as usize] = Some(value);
17+
let mut res = Vec::new();
18+
while self.ptr < self.vals.len() {
19+
if let Some(s) = &self.vals[self.ptr] {
20+
res.push(s.clone());
21+
self.ptr += 1;
22+
} else {
23+
break;
24+
}
25+
}
26+
res
27+
}
28+
}
29+
30+
/**
31+
* Your OrderedStream object will be instantiated and called as such:
32+
* let obj = OrderedStream::new(n);
33+
* let ret_1: Vec<String> = obj.insert(idKey, value);
34+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class OrderedStream {
2+
private ptr: number;
3+
private vals: string[];
4+
5+
constructor(n: number) {
6+
this.ptr = 0;
7+
this.vals = new Array(n);
8+
}
9+
10+
insert(idKey: number, value: string): string[] {
11+
this.vals[idKey - 1] = value;
12+
const res = [];
13+
while (this.vals[this.ptr] != null) {
14+
res.push(this.vals[this.ptr]);
15+
this.ptr++;
16+
}
17+
return res;
18+
}
19+
}
20+
21+
/**
22+
* Your OrderedStream object will be instantiated and called as such:
23+
* var obj = new OrderedStream(n)
24+
* var param_1 = obj.insert(idKey,value)
25+
*/

0 commit comments

Comments
 (0)
Please sign in to comment.