CC150 : Queue by Two Stacks
两个stack来实现queue,相似的是两个queue也能实现stack。
class Solution { public Stack<Integer> sortStack(Stack<Integer> s1) { Stack<Integer> s2 = new Stack<Integer>(); while(!s1.isEmpty()) { int temp = s1.pop(); while(!s2.isEmpty() && s2.peek() > temp) { s1.push(s2.pop()); } s2.push(temp); } return s2; } }

浙公网安备 33010602011771号