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 }

浙公网安备 33010602011771号