Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ff09bdc

Browse files
authored
Update _225.java
1 parent 9c593b9 commit ff09bdc

File tree

1 file changed

+63
-49
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+63
-49
lines changed
Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,67 @@
1-
package com.fishercoder.solutions;
2-
31
import java.util.LinkedList;
42
import java.util.Queue;
5-
/**
6-
* 225. Implement Stack using Queues
7-
*
8-
* Implement the following operations of a stack using queues.
9-
10-
push(x) -- Push element x onto stack.
11-
pop() -- Removes the element on top of the stack.
12-
top() -- Get the top element.
13-
empty() -- Return whether the stack is empty.
14-
15-
Notes:
16-
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.
17-
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.
18-
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
19-
Update (2015-06-11):
20-
The class name of the Java function had been updated to MyStack instead of Stack.*/
21-
22-
public class _225 {
23-
24-
public static class Solution1 {
25-
class MyStack {
26-
27-
Queue<Integer> q = new LinkedList();
28-
29-
// Push element x onto stack.
30-
public void push(int x) {
31-
q.offer(x);
32-
for (int i = 1; i < q.size(); i++) {
33-
q.offer(q.remove());
34-
}
35-
}
36-
37-
// Removes the element on top of the stack.
38-
public void pop() {
39-
q.poll();
40-
}
41-
42-
// Get the top element.
43-
public int top() {
44-
return q.peek();
45-
}
463

47-
// Return whether the stack is empty.
48-
public boolean empty() {
49-
return q.isEmpty();
50-
}
51-
}
52-
}
4+
public class MyStack {
5+
public Queue<Integer> queue1;
6+
public Queue<Integer> queue2;
7+
public int flag;
8+
public int size;
9+
public MyStack() {
10+
queue1=new LinkedList<Integer>();
11+
queue2=new LinkedList<Integer>();
12+
flag=1;
13+
size=0;
14+
}
15+
16+
public void push(int x) {
17+
if(flag==1){
18+
queue1.offer(x);
19+
}else{
20+
queue2.offer(x);
21+
}
22+
size++;
23+
}
24+
25+
public int pop() {
26+
int value;
27+
if(flag==1){
28+
while(queue1.size()>1){
29+
queue2.offer(queue1.poll());
30+
}
31+
value=queue1.poll();
32+
flag=2;
33+
}else{
34+
while(queue2.size()>1){
35+
queue1.offer(queue2.poll());
36+
}
37+
value=queue2.poll();
38+
flag=1;
39+
}
40+
size--;
41+
return value;
42+
}
43+
44+
public int top() {
45+
if(flag==1){
46+
while(queue1.size()>1){
47+
queue2.offer(queue1.poll());
48+
}
49+
int value=queue1.poll();
50+
queue2.offer(value);
51+
flag=2;
52+
return value;
53+
}else{
54+
while(queue2.size()>1){
55+
queue1.offer(queue2.poll());
56+
}
57+
int value=queue2.poll();
58+
queue1.offer(value);
59+
flag=1;
60+
return value;
61+
}
62+
}
63+
64+
public boolean empty() {
65+
return size==0?true:false;
66+
}
5367
}

0 commit comments

Comments
 (0)