码迷,mamicode.com
首页 > 其他好文 > 详细

CC150 : Sort Stack

时间:2015-02-26 09:48:17      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

可以使用一个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 }

 

 

CC150 : Sort Stack

标签:

原文地址:http://www.cnblogs.com/diyishao/p/4300567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!