Skip to content

Commit 6e94fdc

Browse files
authored
Merge pull request soapyigu#251 from ilyarmnzhdn/master
soapyigu#232 Queue question solution
2 parents f0f1885 + 9d0f892 commit 6e94fdc

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Queue/ImplementQueueUsingStacks.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/implement-queue-using-stacks/
3+
* Primary idea: queue
4+
* Time Complexity: O(n), Space Complexity: O(n)
5+
*
6+
* Copyright © 2019 Ilyar Mnazhdin. All rights reserved.
7+
8+
* Your MyQueue object will be instantiated and called as such:
9+
* let obj = MyQueue()
10+
* obj.push(x)
11+
* let ret_2: Int = obj.pop()
12+
* let ret_3: Int = obj.peek()
13+
* let ret_4: Bool = obj.empty()
14+
*/
15+
16+
import Foundation
17+
18+
class MyQueue {
19+
var storage = [Int]()
20+
21+
/** Initialize your data structure here. */
22+
init() {
23+
24+
}
25+
26+
/** Push element x to the back of queue. */
27+
func push(_ x: Int) {
28+
storage.append(x)
29+
}
30+
31+
/** Removes the element from in front of queue and returns that element. */
32+
func pop() -> Int {
33+
return storage.removeFirst()
34+
}
35+
36+
/** Get the front element. */
37+
func peek() -> Int {
38+
guard let first = storage.first else { return 0}
39+
return first
40+
}
41+
42+
/** Returns whether the queue is empty. */
43+
func empty() -> Bool {
44+
return storage.isEmpty
45+
}
46+
}

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* [String](#string)
1818
* [Linked List](#linked-list)
1919
* [Stack](#stack)
20+
* [Queue](#queue)
2021
* [Tree](#tree)
2122
* [Dynamic programming](#dynamic-programming)
2223
* [Depth-first search](#depth-first-search)
@@ -163,6 +164,10 @@
163164
[Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)| [Swift](./Stack/PostorderTraversal.swift)| Hard| O(n)| O(n)|
164165
[Decode String](https://leetcode.com/problems/decode-string/)| [Swift](./Stack/DecodeString.swift)| Medium| O(n)| O(n)|
165166

167+
## Queue
168+
| Title | Solution | Difficulty | Time | Space |
169+
| ----- | -------- | ---------- | ---- | ----- |
170+
[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks)| [Swift](./Queue/ImplementQueueUsingStacks.swift)| Easy| O(n)| O(n)|
166171

167172
## Tree
168173
| Title | Solution | Difficulty | Time | Space |
@@ -640,7 +645,7 @@
640645
| | 235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) | Easy |
641646
| [Swift](./LinkedList/PalindromeLinkedList.swift) | 234 | [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) | Easy |
642647
| | 233 | [Number of Digit One](https://leetcode.com/problems/number-of-digit-one/) | Hard |
643-
| | 232 | [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) | Easy |
648+
| [Swift](./Queue/ImplementQueueUsingStacks.swift) | 232 | [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) | Easy |
644649
| [Swift](./Math/PowerTwo.swift) | 231 | [Power of Two](https://leetcode.com/problems/power-of-two/) | Easy |
645650
| [Swift](./Tree/KthSmallestElementBST.swift) | 230 | [Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) | Medium |
646651
| [Swift](./Array/MajorityElementII.swift) | 229 | [Majority Element II](https://leetcode.com/problems/majority-element-ii/) | Medium |

0 commit comments

Comments
 (0)