//	Write a program to sort a stack in ascending order. You should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to write this program: push | pop | peek | isEmpty.
//
// 使用一个临时stack
#include <iostream>
#include <stack>
using namespace std;
class myStack
{
public:
stack<int>s;
public:
myStack(){} void sort()
{
stack<int>buffer;
while(!s.empty())
{
if (buffer.empty())
{
buffer.push(s.top());
s.pop();
}
else
{
int t = s.top();
s.pop();
while(!buffer.empty() && t > buffer.top())/***注意&&左右判别式的先后***/
{
s.push(buffer.top());
buffer.pop();
}
buffer.push(t);
}
}
while(!buffer.empty())
{
s.push(buffer.top());
buffer.pop();
}
}
}; int main()
{
myStack st;
for (int i =0; i<10; i++)
{
st.s.push(i*((i%2==0)?1:-1));
}
st.sort();
cout<<endl;
return 0;
}

Cracking The Coding Interview 3.6的更多相关文章

  1. Cracking the coding interview

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

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

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

  3. Cracking the Coding Interview(Trees and Graphs)

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

  4. 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 ...

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

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

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

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

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

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

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

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. js数组,字符串,json互相转换函数有哪些

    js数组,字符串,json互相转换函数有哪些 一.总结 一句话总结: JSON.stringify(arr) JSON.parse(jsonString) str.split('') array.jo ...

  2. 雷林鹏分享:jQuery EasyUI 树形菜单 - 使用标记创建树形菜单

    jQuery EasyUI 树形菜单 - 使用标记创建树形菜单 一个树形菜单(Tree)可以从标记创建.easyui 树形菜单(Tree)也可以定义在 元素中.无序列表的 元素提供一个基础的树(Tre ...

  3. Nr,GenBank, RefSeq, UniProt 数据库的异同

    Nr,GenBank, RefSeq, UniProt 数据库的异同 有的文章在做DEG分析时,会把reads比对到RefSeq的转录组上.我也没搞清楚这和直接比对到常规转录组上有什么区别. 文章:S ...

  4. win下使用VM虚拟机安装Linux系统

    自己电脑上还是有个自己的虚拟机比较方便,之前用的Ubuntu,发现卡得不行. 现在装了个轻量级的Lubuntu,速度提升了不少. 1.下载Lubuntu,安装. 2.进入,设置root密码,初始化ro ...

  5. Python3之JSON数据解析实例:新闻头条 --Python3

    一.接口相关 数据服务商:聚合数据(https://www.juhe.cn/) API部分文档: 完整API文档下载:https://files.cnblogs.com/files/qikeyishu ...

  6. 关于OkHttp同步请求的小错误

    今天进行OkHttp的同步请求 写的都是按照官方的去写的 但是返回的东西却不是我想要的 原因是我直接拿到Response后,直接Response.toString,想要拿到返回值 但是这样是错误的,正 ...

  7. jenkins+maven+git+tomcat+salt自动构建

    jenkins 安装步骤 环境: centos7,jdk1.8.0,jenkins2.165,maven3.5.2 一. 安装配置java环境   JDK1.8下载地址:http://www.orac ...

  8. Composer 的学习

    一.Composer简介 Composer 是PHP用来管理依赖关系的工具. 使用 composer 的必要前提有: 1.PHP版本要高于PHP5.3.2 2.PHP支持OpenSSL扩展 3.安装有 ...

  9. java使用线程设置定时任务

    private static int a=0; public static void main( String[] args ) { timer(); } public static void tim ...

  10. windows中安装liunx虚拟机

    我感觉这个写的很好,所以本内容是转他人的内容. http://www.csdn.net/article/2015-06-05/2824882/1