双栈排序(Java)
2015-08-25 11:12
316 查看
题目描述
请编写一个程序,按升序对栈进行排序(即最大元素位于栈顶),要求最多只能使用一个额外的栈存放临时数据,但不得将元素复制到别的数据结构中。给定一个int[] numbers(C++中为vector<int>),其中第一个元素为栈顶,请返回排序后的栈。请注意这是一个栈,意味着排序过程中你只能访问到第一个元素。
测试样例:
[1,2,3,4,5]
返回:[5,4,3,2,1]
import java.util.Stack; import java.util.ArrayList; public class TwoStacks { public ArrayList<Integer> twoStacksSort(int[] numbers) { // write code here Stack<Integer> stack = new Stack<Integer>(); Stack<Integer> stack1 = new Stack<Integer>(); for(int i=0; i<numbers.length; i++){ stack.push(numbers[i]); } while(!stack.isEmpty()){ int tmp = stack.pop().intValue(); while(!stack1.isEmpty() && stack1.peek() > tmp){ stack.push(stack1.pop()); } stack1.push(tmp); } ArrayList<Integer> tmp1 = new ArrayList<Integer>(); while(!stack1.isEmpty()){ tmp1.add(stack1.pop()); } return tmp1; } public Stack<Integer> twoStacksSort1(Stack<Integer> s) { // write code here Stack<Integer> stack = new Stack<Integer>(); while(!s.isEmpty()){ int tmp = s.pop(); while(!stack.isEmpty() && stack.peek() > tmp){ s.push(stack.pop()); } stack.push(tmp); } return stack; } public static void main(String[] args) { TwoStacks ts = new TwoStacks(); int [] numbers = {1,3,2,6,5}; ArrayList<Integer> al = ts.twoStacksSort(numbers); for(int i=0; i<al.size(); i++){ System.out.print(al.get(i) + " "); } } }
[/code]
相关文章推荐
- Swap in JAVA, 不是想象中的简单
- java 后台封装json数据
- java编码格式
- 摘抄笔记--《编写高质量代码:改善Java程序的151个建议》
- eclipse常用快捷键
- 《Java实战开发经典》第五章5.5
- JAVASE从零开始1
- Java序列化机制和原理
- spring定时器 定时操作同一数据库数据
- Java中的泛型
- Spring3.X学习笔记
- Java IO(一)
- Java知识总结---MVC模式解析之MyBatis(四)
- java中Collection类及其子类
- spring定时器
- 二叉树后序排列Binary Tree Postorder Traversal (Java)
- SpringContextHolder.java工具类
- Spring之FactoryBean
- 二叉树中序遍历Binary Tree Inorder Traversal (Java)
- Java中正则表达式及其常用类Math、Calendar、Date、BigDecimal、BigInterger、System、Rondom的使用