快手面试手撕的代码,由于输入输出没搞明白,本来会的题撕得不太顺利,记录在此,共同学习!
import java.util.*;
public class TwoStack {
public static void main(String[] args) {
Solution solution = new Solution();
//s1:0 1 2 3 4
for (int i = 0; i < 5; i++) {
solution.s1.push(i);
}
solution.push(5);//s1:0 1 2 3 4 5
//按照队列的先进先出原则,应该输出0 1 2 3 4 5
for (int i = 0; i < 6; i++) {
System.out.print(solution.pop()+" ");
}
}
}
class Solution{
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
public void push(int a){
s1.push(a);
}
public int pop(){
if(s2.size()==0){
while(s1.size()!=0){
s2.push(s1.pop());
}
}
return s2.pop();
}
}