2014-03-19 03:01

题目:给定一个栈,设计一个算法,在只使用栈操作的情况下将其排序。你可以额外用一个栈。排序完成后,最大元素在栈顶。

解法:我在草稿纸上试了试{1,4,2,3}之类的小例子,大概两三分钟有了思路。既然比较性排序是基于比较和交换的,那么就在两个栈的栈顶进行比较,同时在栈顶进行交换,此处需要O(1)的空间来进行交换。具体实现请看代码,时间复杂度为O(n^2)。

代码:

 // 3.6 Try to sort the elements in a stack with the aid of at most one more stack.
#include <cstdio>
#include <stack>
using namespace std; class Solution {
public:
void sortStack(stack<int> &s1) {
if (s1.empty()) {
return;
}
// extra O(n) here
stack<int> s2;
// extra O(1) here
int val; while (!s1.empty()) {
if (s2.empty() || s1.top() <= s2.top()) {
s2.push(s1.top());
s1.pop();
} else {
val = s1.top();
s1.pop();
while (!s2.empty() && s2.top() < val) {
s1.push(s2.top());
s2.pop();
}
s2.push(val);
}
}
while (!s2.empty()) {
s1.push(s2.top());
s2.pop();
}
}
}; int main()
{
Solution sol;
int i, n, val;
stack<int> st; while (scanf("%d", &n) == && n > ) {
for (i = ; i < n; ++i) {
scanf("%d", &val);
st.push(val);
}
sol.sortStack(st); printf("%d", st.top());
st.pop();
while (!st.empty()) {
printf(" %d", st.top());
st.pop();
}
printf("\n");
} return ;
}

《Cracking the Coding Interview》——第3章:栈和队列——题目6的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 数据结构(c语言版,严蔚敏)第3章栈和队列

    第3章栈和队列

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. Cracking the Coding Interview 150题(二)

    3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...

  10. 数据结构(C语言版)-第3章 栈和队列

    3.1 栈和队列的定义和特点3.2 案例引入3.3 栈的表示和操作的实现3.4 栈与递归3.5 队列的的表示和操作的实现3.6 案例分析与实现 基本操作有入栈.出栈.读栈顶元素值.建栈.判断栈满.栈空 ...

随机推荐

  1. April 5 2017 Week 14 Wednesday

    Today is a perfect day to start living your dream. 实现梦想,莫如当下. Miracles may happen every day. If you ...

  2. **611. Valid Triangle Number three pointer O(n^3) -> square(binary search larget number smaller than target)

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

  3. python学习接口测试(二)

    .python接口之http请求 python的强大之处在于提供了很多的标准库以及第三库,本文介绍urllib 和第三库的requests. Urllib 定义了很多函数和类,这些函数和类能够帮助我们 ...

  4. Docker中的三个基本概念容器(container)、镜像(image)和仓库(registry)之间有什么关系?

    Docker镜像是一个特殊的文件系统,除了提供容器运行时所需的程序.库.资源.配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷.环境变量.用户等).镜像不包含任何动态数据,其内容在构建之 ...

  5. js实现弹窗一个ip在24小时只弹出一次的代码

    function cookieGO(name) { var today = new Date(); var expires = new Date(); expires.setTime(today.ge ...

  6. mask r-cnn

    mask R-cnn, kaiming he的新作.可以同时完成object detection和segmentation,还可以做pose estimation,简直就是功能多多啊.在coco上测试 ...

  7. C# grid控件用数据库分页后台怎么写?

    C#grid控件使用数据库分页的写法如下: mySystem.GetDataa(gridName.PageIndex *gridName.PageSize + 1, (gridName.PageInd ...

  8. Core Location :⽤用于地理定位

    Core Location :⽤用于地理定位 在移动互联⽹网时代,移动app能解决⽤用户的很多⽣生活琐事,⽐比如 导航:去任意陌⽣生的地⽅方 周边:找餐馆.找酒店.找银⾏行.找电影院 在上述应⽤用中, ...

  9. Mac openssl 和curl源码编译

    1.先编译openssl, 下载源码后解压,终端进入源码目录,输入命令配置编译环境:./Configure darwin64-x86_64-cc 等待配置完成后,输入make  和make insta ...

  10. Inventory Update-freecodecamp算法题目

    Inventory Update 1.要求 依照一个存着新进货物的二维数组,更新存着现有库存(在 arr1 中)的二维数组. 如果货物已存在则更新数量 . 如果没有对应货物则把其加入到数组中,更新最新 ...