Skip to content

Commit

Permalink
Merge pull request soapyigu#251 from ilyarmnzhdn/master
Browse files Browse the repository at this point in the history
soapyigu#232 Queue question solution
  • Loading branch information
soapyigu authored Sep 2, 2019
2 parents f0f1885 + 9d0f892 commit 6e94fdc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
46 changes: 46 additions & 0 deletions Queue/ImplementQueueUsingStacks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Question Link: https://leetcode.com/problems/implement-queue-using-stacks/
* Primary idea: queue
* Time Complexity: O(n), Space Complexity: O(n)
*
* Copyright © 2019 Ilyar Mnazhdin. All rights reserved.

* Your MyQueue object will be instantiated and called as such:
* let obj = MyQueue()
* obj.push(x)
* let ret_2: Int = obj.pop()
* let ret_3: Int = obj.peek()
* let ret_4: Bool = obj.empty()
*/

import Foundation

class MyQueue {
var storage = [Int]()

/** Initialize your data structure here. */
init() {

}

/** Push element x to the back of queue. */
func push(_ x: Int) {
storage.append(x)
}

/** Removes the element from in front of queue and returns that element. */
func pop() -> Int {
return storage.removeFirst()
}

/** Get the front element. */
func peek() -> Int {
guard let first = storage.first else { return 0}
return first
}

/** Returns whether the queue is empty. */
func empty() -> Bool {
return storage.isEmpty
}
}
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [String](#string)
* [Linked List](#linked-list)
* [Stack](#stack)
* [Queue](#queue)
* [Tree](#tree)
* [Dynamic programming](#dynamic-programming)
* [Depth-first search](#depth-first-search)
Expand Down Expand Up @@ -163,6 +164,10 @@
[Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)| [Swift](./Stack/PostorderTraversal.swift)| Hard| O(n)| O(n)|
[Decode String](https://leetcode.com/problems/decode-string/)| [Swift](./Stack/DecodeString.swift)| Medium| O(n)| O(n)|

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

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

0 comments on commit 6e94fdc

Please sign in to comment.