CC150 : Sort Stack

可以使用一个extra的stack。
 
 1 class Solution {
 2     public Stack<Integer> sortStack(Stack<Integer> s1) {
 3         Stack<Integer> s2 = new Stack<Integer>();
 4         while(!s1.isEmpty()) {
 5             int temp = s1.pop();
 6             while(!s2.isEmpty() && s2.peek() > temp) {
 7                 s1.push(s2.pop());
 8             }
 9             s2.push(temp);
10         }
11         return s2;
12     }
13 }

 

 

posted @ 2015-02-26 09:27  江南第一少  阅读(143)  评论(0)    收藏  举报