首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。要求函数min、push以及pop的时间复杂度都是O(1)。
定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。要求函数min、push以及pop的时间复杂度都是O(1)。
admin
2014-11-15
158
问题
定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。要求函数min、push以及pop的时间复杂度都是O(1)。
选项
答案
我看到这道题目时,第一反应就是每次push一个新元素时,将栈里所有逆序元素排序。这样栈顶元素将是最小元素。但由于不能保证最后push进栈的元素最先出栈,这种思路设计的数据结构已经不是一个栈了。 在栈里添加一个成员变量存放最小元素(或最小元素的位置)。每次push一个新元素进栈的时候,如果该元素比当前的最小元素还要小,则更新最小元素。 乍一看这样思路挺好的。但仔细一想,该思路存在一个重要的问题:如果当前最小元素被pop出去,如何才能得到下一个最小元素? 因此仅仅只添加一个成员变量存放最小元素(或最小元素的位置)是不够的。我们需要一个辅助栈。每次push一个新元素的时候,同时将最小元素(或最小元素的位置。考虑到栈元素的类型可能是复杂的数据结构,用最小元素的位置将能减少空间消耗)push到辅助栈中;每次pop一个元素出栈的时候,同时pop辅助栈。 参考代码: #include
#include
template
class CStackWithMin { public: CStackWithMin(void) {} virtual ~CStackWithMin(void) {} T& top(void); const T& top(void) const; void push(const T& value); void pop(void); const T& min(void) const; private: T>m_data;// theelements of stack size_t>m_minIndex;// the indicesof minimum elements }; // get the last element of mutable stack template
T& CStackWithMin
::top() { return m_data.back(); } // get the last element of non-mutable stack template
const T& CStackWithMin
::top() const { return m_data.back(); } // insert an elment at the end of stack template
void CStackWithMin
::push(const T& value) { // append the data into the end of m_data m_data.push_back(value); // set the index of minimum elment in m_data at the end of m_minIndex if(m_minIndex.size() == 0) m_minIndex.push_back(0); else { if(value < m_data[m_minIndex.back()]) m_minIndex.push_back(m_data.size() - 1); else m_minIndex.push_back(m_minIndex.back()); } } // erease the element at the end of stack template
void CStackWithMin
::pop() { // pop m_data m_data.pop_back(); // pop m_minIndex m_minIndex.pop_back(); } // get the minimum element of stack template
const T& CStackWithMin
::min() const { assert(m_data.size() > 0); assert(m_minIndex.size() > 0); return m_data[m_minIndex.back()]; } 举个例子演示上述代码的运行过程: 步骤 数据栈 辅助栈 最小值 1.push 3 3 0 3 2.push 4 3,4 0,0 3 3.push 2 3,4,2 0,0,2 2 4.push 1 3,4,2,1 0,0,2,3 1 5.pop 3,4,2 0,0,2 2 6.pop 3,4 0,0 3 7.push 0 3,4,0 0,0,2 0 讨论:如果思路正确,编写上述代码不是一件很难的事情。但如果能注意一些细节无疑能在面试中加分。比如我在上面的代码中做了如下的工作: ? 用模板类实现。如果别人的元素类型只是int类型,模板将能给面试官带来好印象; ? 两个版本的top函数。在很多类中,都需要提供const和非const版本的成员访问函数; ? min函数中assert。把代码写的尽量安全是每个软件公司对程序员的要求; ? 添加一些注释。注释既能提高代码的可读性,又能增加代码量,何乐而不为? 总之,在面试时如果时间允许,尽量把代码写的漂亮一些。说不定代码中的几个小亮点就能让自己轻松拿到心仪的Offer。
解析
转载请注明原文地址:https://kaotiyun.com/show/KDmZ777K
0
程序员面试
相关试题推荐
TheUnitedStatesInterstateHighwaySystemisaninfrastructurefeatofunprecedentedproportions.Notonlydoesitjoinallfi
"Thecatdoesnotofferservices,"WilliamBurroughswrote."Thecatoffersitself."Butitdoessowithunapologeticcontradict
"Thecatdoesnotofferservices,"WilliamBurroughswrote."Thecatoffersitself."Butitdoessowithunapologeticcontradict
Weakdollarorno,$46,000—thepriceforasingleyearofundergraduateinstructionamidtheredbrickofHarvardYard—is【C1】__
输入一个正数n,输出所有和为n连续正数序列。例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5、4-6和7-8。
设置发送邮件服务器的帐户名bob1和密码20022002。
请在MSN共享文件会话界面上,利用工具栏添加共享文件,文件位置为“我的文档\注册.txt”。
请对工作簿Book1设置密码123456,同时对其结构进行保护。
IT服务团队建设周期中,组建期有四个关键步骤,其前后顺序不能改变。现将次序打乱为:①确定目标②稳定核心成员③了解现状④建立团队价值观下面______是其正确的排序方式。
下列()不属于项目群的生命周期的一个阶段。
随机试题
在SNMP报文数据部分中,【】用于指明一个或多个变量的名和对应的值。
城市公共安全最大的威胁是【】
某大厦建筑总面积8000m2,房地总价值8000万元,其中土地总价值3000万元。若某人拥有该大厦的某一部分,该部分的建筑面积200m2,房地价值215万元,则按建筑面积和房地价值进行分摊所计算的该人的土地占有份额为()%。
大体积混凝土温度裂缝包括()。
下列关于一般存款账户的表述中,不正确的是()。
2005年同期农民现金人均收入是()。在农民人均收入中,农民出售农产品收入约是农民家庭第二、三产业收入的()倍。
(中央财大2016)优先股的股权特性和债权特性体现在哪些方面,哪些企业适合采用优先股融资?
WhydopeoplereadnegativeInternetcommentsanddootherthingsthatwillobviouslybepainful?Becausehumanshaveaninheren
ThewordlaserwascoinedasanacronymforLightAmplificationbytheStimulatedEmissionofRadiation.Ordinarylight,fromth
Sofar,therobotseemstobethebest,servantinthehouse.Youcan【B1】______ittodifferenttasks:polishthefloors,dustt
最新回复
(
0
)