Skip to content

Commit 479513d

Browse files
added "1670. Design Front Middle Back Queue"
1 parent 9ab56e5 commit 479513d

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Solutions from [LeetCode](https://leetcode.com) on Dart.
55
## Easy
66

77
| Name | LeetCode | Solution |
8-
|---------------------------------------------|------------------------------------------------------------------------------|---------------------------------------------------------------|
8+
| ------------------------------------------- | ---------------------------------------------------------------------------- | ------------------------------------------------------------- |
99
| 9. Palindrome Number | [Link](https://leetcode.com/problems/palindrome-number/) | [Link](./lib/easy/palindrome_number.dart) |
1010
| 26. Remove Duplicates from Sorted Array | [Link](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Link](./lib/easy/remove_duplicates_from_sorted_array.dart) |
1111
| 27. Remove Element | [Link](https://leetcode.com/problems/remove-element/) | [Link](./lib/easy/remove_element.dart) |
@@ -33,16 +33,17 @@ Solutions from [LeetCode](https://leetcode.com) on Dart.
3333
## Medium
3434

3535
| Name | LeetCode | Solution |
36-
|----------------------------------------|-------------------------------------------------------------------------|------------------------------------------------------------|
36+
| -------------------------------------- | ----------------------------------------------------------------------- | ---------------------------------------------------------- |
3737
| 12. Integer to Roman | [Link](https://leetcode.com/problems/integer-to-roman/) | [Link](./lib/medium/integer_to_roman.dart) |
3838
| 222. Count Complete Tree Nodes | [Link](https://leetcode.com/problems/count-complete-tree-nodes/) | [Link](./lib/medium/count_complete_tree_nodes.dart) |
3939
| 230. Kth Smallest Element in a BST | [Link](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) | [Link](./lib/medium/kth_smallest_element_in_a_bst.dart) |
4040
| 328. Odd Even Linked List | [Link](https://leetcode.com/problems/odd-even-linked-list/) | [Link](./lib/medium/odd_even_linked_list.dart) |
4141
| 1325. Delete Leaves With a Given Value | [Link](https://leetcode.com/problems/delete-leaves-with-a-given-value/) | [Link](./lib/medium/delete_leaves_with_a_given_value.dart) |
42+
| 1670. Design Front Middle Back Queue | [Link](https://leetcode.com/problems/design-front-middle-back-queue/) | [Link](./lib/medium/design_front_middle_back_queue.dart) |
4243

4344
## Hard
4445

4546
| Name | LeetCode | Solution |
46-
|-------------------------------|------------------------------------------------------------------|---------------------------------------------------|
47+
| ----------------------------- | ---------------------------------------------------------------- | ------------------------------------------------- |
4748
| 23. Merge k Sorted Lists | [Link](https://leetcode.com/problems/merge-k-sorted-lists/) | [Link](./lib/hard/merge_k_sorted_lists.dart) |
4849
| 32. Longest Valid Parentheses | [Link](https://leetcode.com/problems/longest-valid-parentheses/) | [Link](./lib/hard/longest_valid_parentheses.dart) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class FrontMiddleBackQueue {
2+
final _list = [];
3+
4+
void pushFront(int val) {
5+
_list.insert(0, val);
6+
}
7+
8+
void pushMiddle(int val) {
9+
_list.insert(_list.length ~/ 2, val);
10+
}
11+
12+
void pushBack(int val) {
13+
_list.add(val);
14+
}
15+
16+
int popFront() {
17+
if (_list.isEmpty) {
18+
return -1;
19+
}
20+
21+
return _list.removeAt(0);
22+
}
23+
24+
int popMiddle() {
25+
if (_list.isEmpty) {
26+
return -1;
27+
}
28+
29+
var size = _list.length;
30+
var index = size % 2 == 0 ? size ~/ 2 - 1 : size ~/ 2;
31+
32+
return _list.removeAt(index);
33+
}
34+
35+
int popBack() {
36+
if (_list.isEmpty) {
37+
return -1;
38+
}
39+
40+
return _list.removeLast();
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import 'package:leetcode_dart/medium/design_front_middle_back_queue.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
group(
6+
'Example tests',
7+
() {
8+
test(
9+
'Example test',
10+
() {
11+
FrontMiddleBackQueue q = FrontMiddleBackQueue();
12+
q.pushFront(1);
13+
q.pushBack(2);
14+
q.pushMiddle(3);
15+
q.pushMiddle(4);
16+
17+
expect(1, q.popFront());
18+
expect(3, q.popMiddle());
19+
expect(4, q.popMiddle());
20+
expect(2, q.popBack());
21+
expect(-1, q.popFront());
22+
},
23+
);
24+
},
25+
);
26+
}

0 commit comments

Comments
 (0)