双栈排序
来源:互联网 发布:python 查看字节码 编辑:程序博客网 时间:2024/06/05 02:01
题目描述
请编写一个程序,按升序对栈进行排序(即最大元素位于栈顶),要求最多只能使用一个额外的栈存放临时数据,但不得将元素复制到别的数据结构中。
给定一个int[] numbers(C++中为vector<int>),其中第一个元素为栈顶,请返回排序后的栈。请注意这是一个栈,意味着排序过程中你只能访问到第一个元素。
测试样例:
[1,2,3,4,5]
返回:[5,4,3,2,1]
import java.util.*;public class TwoStacks { public ArrayList<Integer> twoStacksSort(int[] numbers) { // write code here Stack<Integer> stack1 = new Stack(); Stack<Integer> stack2 = new Stack(); for (Integer integer : numbers) {stack1.push(integer);} while(!stack1.isEmpty()){ int tem = stack1.pop(); while(!stack2.isEmpty() && stack2.peek() > tem){ stack1.push(stack2.pop()); } stack2.push(tem); } ArrayList<Integer> list = new ArrayList(); while(!stack2.isEmpty()){ list.add(stack2.pop()); } return list; }}
0 0
- [NOIP2008]双栈排序
- NOIP2008 双栈排序
- sicily双栈排序
- 双栈排序
- [noip2008]双栈排序
- 双栈排序
- Codevs1170 双栈排序
- 双栈排序
- 双栈排序
- 双栈排序
- 双栈排序
- 双栈排序
- 双栈排序
- 双栈排序
- P1155 双栈排序
- 1002. 双栈排序
- 双栈排序
- 双栈排序
- android studio设置成eclipse 快捷键后,平常经常用的快捷键
- 静态链接库与动态链接库
- 2000年分区联赛普级组之三 乘积最大
- 初入操作系统
- 关于Oracle net Manager中点击无法创建监听程序的解决方案
- 双栈排序
- oracle文档
- Bellman-Ford 的动态规划实现
- CentOS下安装软件的常用方法
- Plugin execution not covered by lifecycle configuration
- android音频系统
- eclipse常用快捷键
- 外部引用css时,link和@import两种区别
- JavaScript基础整理--函数 ,对象,作用域等重点