摘要: 题目: 一个栈中元素的类型为整型,现在想将该栈从顶到底按从大到小的顺序排序,只许申请一个栈,除此之外可以申请新的变量,但不能申请额外的数据结构。如何完成排序? 题解: 需要用到辅助栈。stack执行pop操作,弹出元素记为cur; 如果cur小于或等于assist的栈顶元素,则将cur直接压入ass 阅读全文
posted @ 2017-09-11 22:37 Vincent丶丶 阅读(546) 评论(0) 推荐(0) 编辑
摘要: 题目: 一个栈依次压入1、2、3、4、5,那么从栈顶到栈底分别为5、4、3、2、1。将这个栈转置后,从栈顶到栈底为1、2、3、4、5,也就是实现栈中元素的逆序。但是只能用递归函数来实现,不能用其他数据结构 题解: 需要两个递归函数:1. 将栈底元素返回并删除;2. 逆序一个栈 过程即为获取栈底元素, 阅读全文
posted @ 2017-09-11 21:37 Vincent丶丶 阅读(657) 评论(0) 推荐(0) 编辑
摘要: 题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in f 阅读全文
posted @ 2017-09-11 20:57 Vincent丶丶 阅读(123) 评论(0) 推荐(0) 编辑
摘要: 题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- R 阅读全文
posted @ 2017-09-11 20:10 Vincent丶丶 阅读(182) 评论(0) 推荐(0) 编辑