Skip to content

Commit 5e073d4

Browse files
refactor 225
1 parent 36f19e6 commit 5e073d4

File tree

1 file changed

+27
-20
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+27
-20
lines changed

src/main/java/com/fishercoder/solutions/_225.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
import java.util.LinkedList;
44
import java.util.Queue;
5-
/**Implement the following operations of a stack using queues.
5+
/**
6+
* 225. Implement Stack using Queues
7+
*
8+
* Implement the following operations of a stack using queues.
69
710
push(x) -- Push element x onto stack.
811
pop() -- Removes the element on top of the stack.
912
top() -- Get the top element.
1013
empty() -- Return whether the stack is empty.
14+
1115
Notes:
1216
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.
1317
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.
@@ -16,31 +20,34 @@ You may assume that all operations are valid (for example, no pop or top operati
1620
The class name of the Java function had been updated to MyStack instead of Stack.*/
1721

1822
public class _225 {
19-
class MyStack {
2023

21-
Queue<Integer> q = new LinkedList();
24+
public static class Solution1 {
25+
class MyStack {
26+
27+
Queue<Integer> q = new LinkedList();
2228

23-
// Push element x onto stack.
24-
public void push(int x) {
25-
q.offer(x);
26-
for (int i = 1; i < q.size(); i++) {
27-
q.offer(q.remove());
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+
}
2835
}
29-
}
3036

31-
// Removes the element on top of the stack.
32-
public void pop() {
33-
q.poll();
34-
}
37+
// Removes the element on top of the stack.
38+
public void pop() {
39+
q.poll();
40+
}
3541

36-
// Get the top element.
37-
public int top() {
38-
return q.peek();
39-
}
42+
// Get the top element.
43+
public int top() {
44+
return q.peek();
45+
}
4046

41-
// Return whether the stack is empty.
42-
public boolean empty() {
43-
return q.isEmpty();
47+
// Return whether the stack is empty.
48+
public boolean empty() {
49+
return q.isEmpty();
50+
}
4451
}
4552
}
4653
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy