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;
    }
}

 

 
posted @ 2015-02-24 11:24  江南第一少  阅读(132)  评论(0)    收藏  举报