之前做过,还是忘了最佳思路
https://blog.nowcoder.net/n/055faef37a4f47d2a88917ca902002e3
1.其实 不用 每次都弹一遍, 只要判断 2 不为空 就可以一直弹2
2空了 再把1中的弹进来就好了
2.Stack<integer> stack2 = new Stack<integer>();</integer></integer>
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
int res = 0;
while(stack1.size() != 1){
stack2.push(stack1.pop());
}
res = stack1.pop();
while(!stack2.empty()){
stack1.push(stack2.pop());
}
return res;
}
}


京公网安备 11010502036488号