minStack实现
设计包含 min 函数的栈(栈)
定义栈的数据结构,要求添加一个 min 函数,能够得到栈的最小元素。
要求函数 min、push 以及 pop 的时间复杂度都是 O(1)。
#include <algorithm>
#include <iostream>
#include <exception>
#include <string> class illegalParameterValue
{
public:
illegalParameterValue():errorMessage("Illegal parameter value"){}
illegalParameterValue(const char* theMessage){ errorMessage = theMessage;}
std::string errorMessage;
}; template<typename T>
struct minStackElement{
T element;
T min;
}; template<typename T>
class minStack{
private:
int top;
int size;
minStackElement<T>* arrayStack;
public:
minStack(int initCapacity)
{
try{
if(initCapacity < )
throw illegalParameterValue("initCapacity must greater than 0");
}catch(illegalParameterValue& e){
std::cout << "initCapacity has been made to be 1" << std::endl;
initCapacity = ;
}
arrayStack = new minStackElement<T>[initCapacity];
size = initCapacity;
top = -;
}
~minStack()
{
delete [] arrayStack;
}
void push(T x)
{
minStackElement<T> e;
e.element = x;
if(top == -){
e.min = x;
}else if(x < arrayStack[top].min){
e.min = x;
}else{
e.min = arrayStack[top].min;
}
if(top == size-)
{
minStackElement<T>* temp = arrayStack;
arrayStack = new minStackElement<T>[*size];
std::copy(temp,temp+size-,arrayStack);
delete [] temp;
}
arrayStack[++top] = e;
} void pop()
{
if(top == -)
throw illegalParameterValue("the stack is empty");
top--;
} T min()
{
return arrayStack[top].min;
}
};
minStack实现的更多相关文章
- LintCode MinStack
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- MinStack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- leetcode算法学习----155. 最小栈(MinStack )
下面题目是LeetCode算法155题: https://leetcode.com/problems/min-stack/ 题目1:最小函数min()栈 设计一个支持 push,pop,top 操作, ...
- [LeetCode] 155. minStack 设计最小栈
注意:getMin()时间复杂度为O(1) 最原始的方法: class MinStack(object): def __init__(self): """ initial ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- [ACM训练] 算法初级 之 数据结构 之 栈stack+队列queue (基础+进阶+POJ 1338+2442+1442)
再次面对像栈和队列这样的相当基础的数据结构的学习,应该从多个方面,多维度去学习. 首先,这两个数据结构都是比较常用的,在标准库中都有对应的结构能够直接使用,所以第一个阶段应该是先学习直接来使用,下一个 ...
随机推荐
- centos mysql忘记密码找回(仅限mysql5.7)
1.停掉mysql 2.执行#mysqld_safe --user=mysql --skip-grant-tables --skip-networking & 3.#mysql 4.updat ...
- php的循环与引用的一个坑
上代码 $arr = array( 'a'=> 'a11', 'b'=> 'b22', 'c'=> 'c33', ); foreach ($arr as $k=>&$v ...
- Javascript设计模式理论与实战:桥接模式
桥接模式将抽象部分与实现部分分离开来,使两者都可以独立的变化,并且可以一起和谐地工作.抽象部分和实现部分都可以独立的变化而不会互相影响,降低了代码的耦合性,提高了代码的扩展性. 基本理论 桥接模式定义 ...
- Windows10 下 github ssh 访问出现 Permission denied(publickey)错误的解决方法
Windows10 下 github ssh 访问出现 Permission denied(publickey)错误的解决方法. 错误信息: git clone git@github.com:ediw ...
- 一起学习《C#高级编程》2--比较对象的相等性
今后争取每两天能更新一次.平日的诱惑太多,双休只顾玩了,进度有点慢. 接上一讲的,类型的安全性,留下了点小尾巴——比较对象的相等性. C#有四种比较相等的方式:除了“==”运算符外,System.Ob ...
- 数据库工具SQLite Expert Personal的简单使用
官方网站: sqliteexpert官方网址 - SQLite administration | SQLite Expert 先使用SQLite Expert Personal熟悉SQLite语句 插 ...
- c# 后台GET、POST、PUT、DELETE传输发送json数据
一.Get 方式传输 //url为请求的网址,param参数为需要查询的条件(服务端接收的参数,没有则为null) //返回该次请求的响应 public string HttpGet(string u ...
- 8-网络请求之http
本篇博客对应视频讲解 回顾 上一篇讲了Linq的使用,大家自己上手实践之后,相信一定会感到非常快捷方便.更多详细的内容还是需要自己去阅读官方文档. 今天要讲网络请求中的http请求,这也是在编程当中经 ...
- Eclipes批量创建文件夹
package file; import java.io.File; import java.io.IOException; import java.util.Scanner; public clas ...
- shell脚本报错:-bash: xxx: /bin/bash^M: bad interpreter: No such file or directory
当我们把文件从windows系统中编辑的文件拷贝到linux系统中,如果我们执行文件会保存如下的错: shell脚本报错:-bash: xxx: /bin/bash^M: bad interprete ...