Implement Stack using Queues
Implement the following operations of a stack using queues.
push(x) — Push element x onto stack.
pop() — Removes the element on top of the stack.
top() — Get the top element.
empty() — Return whether the stack is empty.
Notes:
You must use only standard operations of a queue — which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
Three Solutions
- O(1) push, O(n) top, O(n) pop - use two Queues: always push to p1; when pop copy first n-1 items to p2, poll nth item from p1, switch p1 p2; when top, copy first n-1 to p2, peek q1, switch and return
- O(n) push, O(1) top, O(1) pop - use two Queues: always keep pushed item at first in p2, then add p1 to p2
- O(1) push, O(n) top, O(n) pop - use one Queue: push to last of list; when pop offer n-1 item polled to the end of list; when top do the same, return peeked item, then offer the first item to list
|
|
Implement Queue using Stacks
Implement the following operations of a queue using stacks.
push(x) — Push element x to the back of queue.
pop() — Removes the element from in front of queue.
peek() — Get the front element.
empty() — Return whether the queue is empty.
Notes:
You must use only standard operations of a stack — which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
Solution
- Simple two stack solution…
|
|
One stack Solution
- (Function Call Stack)
|
|
Add and Search Word - Data structure design
Design a data structure that supports the following two operations:
void addWord(word)
bool search(word)
search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.
For example:
addWord(“bad”)
addWord(“dad”)
addWord(“mad”)
search(“pad”) -> false
search(“bad”) -> true
search(“.ad”) -> true
search(“b..”) -> true
Note:
You may assume that all words are consist of lowercase letters a-z.
click to show hint.
You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first.
Solution
- One solution would be to keep a Map of word length and list of words, this way when we search on a string s, we need to compare the string with each string in the list of same length
- Use Trie, when
|
|