Skip to content

Commit d03cae6

Browse files
flatten 2d vector
1 parent e356669 commit d03cae6

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package medium;
2+
3+
import java.util.Iterator;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
8+
public class Flatten2DVector {
9+
10+
}
11+
12+
class Vector2D implements Iterator<Integer> {
13+
private Queue<Integer> cache;
14+
private List<List<Integer>> vec2d;
15+
16+
public Vector2D(List<List<Integer>> vec2d) {
17+
this.vec2d = vec2d;
18+
this.cache = new LinkedList<Integer>();
19+
if(vec2d != null && vec2d.size() > 0){
20+
for(List<Integer> list : vec2d){
21+
for(int i : list){
22+
cache.offer(i);
23+
}
24+
}
25+
}
26+
}
27+
28+
@Override
29+
public Integer next() {
30+
return cache.poll();
31+
}
32+
33+
@Override
34+
public boolean hasNext() {
35+
return !cache.isEmpty();
36+
}
37+
}
38+

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
|259|[3Sum Smaller](https://leetcode.com/problems/3sum-smaller/)|[Solution](../../blob/master/MEDIUM/src/medium/_3Sum_Smaller.java)| O(n^2)|O(1) | Medium|
6464
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)|[Solution](../../blob/master/EASY/src/easy/BinaryTreePaths.java) | O(n*h) | O(h) | DFS/Recursion
6565
|252|[Meeting Rooms](https://leetcode.com/problems/meeting-rooms/)|[Solution](../../blob/master/EASY/src/easy/MeetingRooms.java) | O(nlogn) | O(1) |
66+
|251|[Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector/)|[Solution](../../blob/master/MEDIUM/src/medium/Flatten2DVector.java)| O(1)|O(m*n) | Medium|
6667
|250|[Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees/)|[Solution](../../blob/master/MEDIUM/src/medium/CountUnivalueSubtrees.java)| O(n)|O(h) | Medium| DFS
6768
|249|[Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/)|[Solution](../../blob/master/EASY/src/easy/GroupShiftedStrings.java) | O(nlogn) | O(n) |
6869
|246|[Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/)|[Solution](../../blob/master/EASY/src/easy/StrobogrammaticNumber.java) | O(n) | O(1) |

0 commit comments

Comments
 (0)