Created
April 12, 2018 16:02
-
-
Save s4553711/64c4e75f798d5125a3f8a9f5ed3f949b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyQueue { | |
public: | |
/** Initialize your data structure here. */ | |
MyQueue() { | |
} | |
/** Push element x to the back of queue. */ | |
void push(int x) { | |
while(!s2.empty()) { | |
s1.push(s2.top()); | |
s2.pop(); | |
} | |
s1.push(x); | |
} | |
/** Removes the element from in front of queue and returns that element. */ | |
int pop() { | |
while(!s1.empty()) { | |
s2.push(s1.top()); | |
s1.pop(); | |
} | |
int result = s2.top(); | |
s2.pop(); | |
return result; | |
} | |
/** Get the front element. */ | |
int peek() { | |
while(!s1.empty()) { | |
s2.push(s1.top()); | |
s1.pop(); | |
} | |
return s2.top(); | |
} | |
/** Returns whether the queue is empty. */ | |
bool empty() { | |
return s1.empty() && s2.empty(); | |
} | |
private: | |
stack<int> s1; | |
stack<int> s2; | |
}; | |
/** | |
* Your MyQueue object will be instantiated and called as such: | |
* MyQueue obj = new MyQueue(); | |
* obj.push(x); | |
* int param_2 = obj.pop(); | |
* int param_3 = obj.peek(); | |
* bool param_4 = obj.empty(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment