首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。要求函数min、push以及pop的时间复杂度都是O(1)。
定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。要求函数min、push以及pop的时间复杂度都是O(1)。
admin
2019-03-29
174
问题
定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。要求函数min、push以及pop的时间复杂度都是O(1)。
选项
答案
#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
解析
这是去年google的一道面试题。
我看到这道题目时,第一反应就是每次push一个新元素时,将栈里所有逆序元素排序。这样栈顶元素将是最小元素。但由于不能保证最后push进栈的元素最先出栈,这种思路设计的数据结构已经不是一个栈了。
在栈里添加一个成员变量存放最小元素(或最小元素的位置)。每次push一个新元素进栈的时候,如果该元素比当前的最小元素还要小,则更新最小元素。
乍一看这样思路挺好的。但仔细一想,该思路存在一个重要的问题:如果当前最小元素被pop出去,如何才能得到下一个最小元素?
因此仅仅只添加一个成员变量存放最小元素(或最小元素的位置)是不够的。我们需要一个辅助栈。每次push一个新元素的时候,同时将最小元素(或最小元素的位置。考虑到栈元素的类型可能是复杂的数据结构,用最小元素的位置将能减少空间消耗)push到辅助栈中;每次pop一个元素出栈的时候,同时pop辅助栈。
转载请注明原文地址:https://kaotiyun.com/show/2xmZ777K
0
程序员面试
相关试题推荐
ForAmerica’schildrentheeducationsystemisoftenliterallyalottery.ThatisthemainmessageofanewdocumentaryaboutAm
"Thecatdoesnotofferservices,"WilliamBurroughswrote."Thecatoffersitself."Butitdoessowithunapologeticcontradict
输入n个整数,输出其中最小的k个。例如输入1,2,3,4,5,6,7和8这8个数字,则最小的4个数字为1,2,3和4。
如果字符串一的所有字符按其在字符串中的顺序出现在另外一个字符串二中,则字符串一称之为字符串二的子串。注意,并不要求子串(字符串一)的字符必须连续出现在字符串二中。请编写一个函数,输入两个字符串,求它们的最长公共子串,并打印出最长公共子串。例如:输入两个
Dreamweaver的视图(View)菜单中,Grid→settings表示______。A.选择计量系统来设定标尺单位B.显示一个应用当前设置的背景网格C.使插入对象与最近的对齐设置对齐D.进行网格设置
请对工作簿Book1设置密码123456,同时对其结构进行保护。
IT服务团队建设周期中,组建期有四个关键步骤,其前后顺序不能改变。现将次序打乱为:①确定目标②稳定核心成员③了解现状④建立团队价值观下面______是其正确的排序方式。
信息系统的生命周期可以简化为立项、开发、运维及消亡四个阶段,()属于开发阶段的工作。
下列()不属于项目群的生命周期的一个阶段。
随机试题
宽胸散结,治疗痰浊痹阻之胸痹,宜首选
心脏病猝死中最常见的原因是
下列不属于流动资产的是()。
Routing:Beijing,CHINA(BJS)toTokyo,JAPAN(TYO)Commodity:MOONCAKEGrossWeight:1Pieces,5.8KgsDimensions
国家出资企业因增资致使国家不再拥有所出资企业控股权的,须由国资监管机构报本级人民政府批准。()
没有物就没有影,事实和思想的关系就是物与影的关系。这种观点是()
InhislecturesattheLyceum,aschoolthatremainedforcenturiesoneofthegreatcentersoflearninginGreece,Aristotlede
Readthetextsfromamagazinearticlestressmanagement.Forquestions61to65,matchthenameofeachpersontooneofthest
Thesimplestwaytosayitisthis:Ibelieveinmymother.My【C1】______beganwhenIwasjustakid.I【C2】______becomingadocto
TheImportanceofJustBeingThere"What’sthemostimportantthingyou’vedoneinyourlife?"Thequestionwasputtomedu
最新回复
(
0
)