parent
726798f24d
commit
03a07e4fe3
@ -0,0 +1,35 @@ |
||||
--- |
||||
栈相关 |
||||
--- |
||||
|
||||
#### [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(); |
||||
} |
||||
} |
||||
} |
||||
``` |
||||
|
Loading…
Reference in new issue