Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit972b0a3

Browse files
refactor 232
1 parentcdce4bc commit972b0a3

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
packagecom.fishercoder.solutions;
22

33
importjava.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
publicclass_232 {
1520

16-
classMyQueue {
21+
publicstaticclassSolution1 {
22+
classMyQueue {
1723

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

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

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

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

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp