You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
685 B
36 lines
685 B
4 years ago
|
---
|
||
|
栈相关
|
||
|
---
|
||
|
|
||
|
#### [09. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)
|
||
|
|
||
|
```java
|
||
|
class CQueue {
|
||
|
|
||
|
Stack<Integer> putStack, takeStack;
|
||
|
|
||
|
public CQueue() {
|
||
|
putStack = new Stack<>();
|
||
|
takeStack = new Stack<>();
|
||
|
}
|
||
|
|
||
|
public void appendTail(int value) {
|
||
|
putStack.push(value);
|
||
|
}
|
||
|
|
||
|
public int deleteHead() {
|
||
|
if (takeStack.isEmpty()) {
|
||
|
while (!putStack.isEmpty()) {
|
||
|
takeStack.push(putStack.pop());
|
||
|
}
|
||
|
}
|
||
|
if (takeStack.isEmpty()) {
|
||
|
return -1;
|
||
|
} else {
|
||
|
return takeStack.pop();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|