Skip to content

Commit 972b0a3

Browse files
refactor 232
1 parent cdce4bc commit 972b0a3

File tree

1 file changed

+32
-24
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+32
-24
lines changed
Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,55 @@
11
package com.fishercoder.solutions;
22

33
import java.util.Stack;
4-
/**Implement the following operations of a queue using stacks.
4+
/**
5+
* 232. Implement Queue using Stacks
6+
*
7+
* Implement the following operations of a queue using stacks.
58
69
push(x) -- Push element x to the back of queue.
710
pop() -- Removes the element from in front of queue.
811
peek() -- Get the front element.
912
empty() -- Return whether the queue is empty.
13+
1014
Notes:
1115
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.
1216
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.
13-
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).*/
17+
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
18+
*/
1419
public class _232 {
1520

16-
class MyQueue {
21+
public static class Solution1 {
22+
class MyQueue {
1723

18-
Stack<Integer> input = new Stack();
19-
Stack<Integer> output = new Stack();
24+
Stack<Integer> input = new Stack();
25+
Stack<Integer> output = new Stack();
2026

21-
// Push element x to the back of queue.
22-
public void push(int x) {
23-
input.push(x);
24-
}
27+
// Push element x to the back of queue.
28+
public void push(int x) {
29+
input.push(x);
30+
}
2531

26-
// Removes the element from in front of queue.
27-
public int pop() {
28-
peek();
29-
return output.pop();
30-
}
32+
// Removes the element from in front of queue.
33+
public int pop() {
34+
peek();
35+
return output.pop();
36+
}
3137

32-
// Get the front element.
33-
public int peek() {
34-
if (output.isEmpty()) {
35-
while (!input.isEmpty()) {
36-
output.push(input.pop());
38+
// Get the front element.
39+
public int peek() {
40+
if (output.isEmpty()) {
41+
while (!input.isEmpty()) {
42+
output.push(input.pop());
43+
}
3744
}
45+
return output.peek();
3846
}
39-
return output.peek();
40-
}
4147

42-
// Return whether the queue is empty.
43-
public boolean empty() {
44-
return input.isEmpty() && output.isEmpty();
48+
// Return whether the queue is empty.
49+
public boolean empty() {
50+
return input.isEmpty() && output.isEmpty();
51+
}
4552
}
4653
}
4754
}
55+

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