lc232-Implement Queue using Stacks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Queue {
public:
stack<int> s1,s2;
void transfer(){
if(s2.empty()){
while(!s1.empty()){
int x = s1.top();
s1.pop();
s2.push(x);
}
}
}
// Push element x to the back of queue.
void push(int x) {
s1.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
transfer();
s2.pop();
}
// Get the front element.
int peek(void) {
transfer();
return s2.top();
}
// Return whether the queue is empty.
bool empty(void) {
return s1.empty() && s2.empty();
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @constructor
*/
var Queue = function() {
this.queue1 = [];
this.queue2 = [];
};
Queue.prototype.transfer = function(){
if (this.queue2.length === 0) {
while (this.queue1.length !== 0){
var x = this.queue1[this.queue1.length-1]
this.queue1.pop()
this.queue2.push(x)
}
}
}
/**
* @param {number} x
* @returns {void}
*/
Queue.prototype.push = function(x) {
this.queue1.push(x)
};
/**
* @returns {void}
*/
Queue.prototype.pop = function() {
this.transfer()
this.queue2.pop()
};
/**
* @returns {number}
*/
Queue.prototype.peek = function() {
this.transfer()
return this.queue2[this.queue2.length-1]
};
/**
* @returns {boolean}
*/
Queue.prototype.empty = function() {
return this.queue1.length === 0 && this.queue2.length === 0
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Queue(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.queue1 = []
self.queue2 = []
def transfer(self):
if len(self.queue2) == 0:
while not len(self.queue1) == 0:
x = self.queue1[-1]
self.queue1 = self.queue1[:-1]
self.queue2.append(x)
def push(self, x):
"""
:type x: int
:rtype: nothing
"""
self.queue1.append(x)
def pop(self):
"""
:rtype: nothing
"""
self.transfer()
self.queue2 = self.queue2[:-1]
def peek(self):
"""
:rtype: int
"""
self.transfer()
return self.queue2[-1]
def empty(self):
"""
:rtype: bool
"""
return self.queue1 == [] and self.queue2 == []
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class MyQueue{
ArrayList<Integer> queue1 = new ArrayList<Integer>();
ArrayList<Integer> queue2 = new ArrayList<Integer>();
public void transfer(){
if(queue2.isEmpty()){
while(!queue1.isEmpty()){
int lastIdex = queue1.size()-1;
Integer x = queue1.get(lastIdex);
queue1.remove(lastIdex);
queue2.add(x);
}
}
}
public void push(int x) {
queue1.add(x);
}
// Removes the element from in front of queue.
public void pop() {
transfer();
queue2.remove(queue2.size()-1);
}
// Get the front element.
public int peek() {
transfer();
return queue2.get(queue2.size()-1);
}
// Return whether the queue is empty.
public boolean empty() {
return queue1.isEmpty() && queue2.isEmpty();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class MyQueue {
ArrayList<Integer> queue = new ArrayList<Integer>();
//Queue queue;
//Stack<Integer> queue;
// Push element x to the back of queue.
public void push(int x) {
queue.add(x);
}
// Removes the element from in front of queue.
public void pop() {
queue.remove(0);
}
// Get the front element.
public int peek() {
return queue.get(0);
}
// Return whether the queue is empty.
public boolean empty() {
return queue.size() == 0;
}
}