《Cracking the Coding Interview》——第3章:栈和队列——题目6
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的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 数据结构(c语言版,严蔚敏)第3章栈和队列
第3章栈和队列
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Cracking the Coding Interview 150题(二)
3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...
- 数据结构(C语言版)-第3章 栈和队列
3.1 栈和队列的定义和特点3.2 案例引入3.3 栈的表示和操作的实现3.4 栈与递归3.5 队列的的表示和操作的实现3.6 案例分析与实现 基本操作有入栈.出栈.读栈顶元素值.建栈.判断栈满.栈空 ...
随机推荐
- Canvas 中drawImage 绘制不出图片
在使用Canvas的drawImage绘制图片时,却发现绘制不出图片,原因是图片是异步加载,图片加载完再绘制. //html <img src="1.png" /> & ...
- Windows计算下载文件的SHA256 MD5 SHA1
引用自 http://blog.163.com/licanli2082@126/blog/static/35748686201284611330/ certutil -hashfile yourfil ...
- 4 - 函数&装饰器 and 迭代器&生成器
函数是什么 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的.程序里函数的定义是: 定义:将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 ...
- Leetcode back(215) to be continue
solution discussion https://leetcode.com/problems/kth-largest-element-in-an-array/description/ -- 21 ...
- iOS开发:小技巧积累
1.获取全局的Delegate对象,这样我们可以调用这个对象里的方法和变量: [(MyAppDelegate*)[[UIApplication sharedApplication] delegate] ...
- Poj(2236),简单并查集
题目链接:http://poj.org/problem?id=2236 思路很简单,傻逼的我输出写成了FALL,然后遍历的时候for循环写错了,还好很快我就Debug出来了. #include < ...
- 5、Oracle备份(oracle备份脚本配置)
1.1 Oracle数据库备份 1.1.1 链接Oracle介质管理库 请在数据库节点上操作. [oracle@db01/usr/openv/netbackup/bin]$ ./oracle_link ...
- Spring常用配置 Scope
Bean的Scope Scope描述的是Spring容器如何新建Bean的实例的.Spring的Scope有以下几种,通过@Scope注解来实现. 1.Singleton:一个Spring容器中 ...
- AI-Info-Micron-Insight:5G、人工智能和即将到来的移动革命
ylbtech-AI-Info-Micron-Insight:5G.人工智能和即将到来的移动革命 1.返回顶部 1. 5G.人工智能和即将到来的移动革命 人们都说自己的手机“智能”,但究竟有多智能?凡 ...
- 前端JavaScript之ECMA
1.JavaScript基础 2.语法规则 3 常用内置对象 4 函数 5 伪数组 6.异常处理 1.1 web前端分为三层 HTML:从语义的角度,描述页面结构 CSS:从审美的角度,描述样式(美化 ...