class MyQueue {
Stack<Integer> stackIn;
Stack<Integer> stackOut;
public MyQueue() {
stackIn = new Stack<>();
stackOut = new Stack<>();
}
public void push(int x) {
stackIn.push(x);
}
public int pop() {
dumpstackIn();
return stackOut.pop();
}
public int peek() {
dumpstackIn();
return stackOut.peek();
}
public boolean empty() {
return stackIn.isEmpty() && stackOut.isEmpty();
}
private void dumpstackIn(){
if (!stackOut.isEmpty()) return;
while (!stackIn.isEmpty()){
stackOut.push(stackIn.pop());
}
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
20. 有效的括号
class Solution {
public boolean isValid(String s) {
Stack<Character> record=new Stack<>();
for(int i=0;i<s.length();i++){
if (s.charAt(i)=='('){
record.push(')');
}
else if (s.charAt(i)=='['){
record.push(']');
}
else if (s.charAt(i)=='{'){
record.push('}');
}
else{
if (record.isEmpty()) return false;
char tmp=record.peek();
if(tmp!=s.charAt(i)) {
return false;
}
else{
record.pop();
}
}
}
return record.isEmpty();
}
}