-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths0251_flatten_2d_vector.rs
60 lines (49 loc) · 1.08 KB
/
s0251_flatten_2d_vector.rs
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
#![allow(unused)]
struct Vector2D {
vector: Vec<i32>,
cur: usize,
len: usize,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl Vector2D {
fn new(v: Vec<Vec<i32>>) -> Self {
let len: usize = v.iter().map(|c| c.len()).sum();
let mut ans = vec![];
for vector in v.into_iter() {
for n in vector {
ans.push(n);
}
}
Self {
vector: ans,
cur: 0,
len: len,
}
}
fn next(&mut self) -> i32 {
if !self.has_next() {
return -1;
}
let ret = self.vector[self.cur];
self.cur += 1;
return ret;
}
fn has_next(&self) -> bool {
self.cur < self.len
}
}
/**
* Your Vector2D object will be instantiated and called as such:
* let obj = Vector2D::new(v);
* let ret_1: i32 = obj.next();
* let ret_2: bool = obj.has_next();
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_346() {}
}