日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

程序员面试题精选100题(02)-设计包含min函数的栈[数据结构]

發(fā)布時(shí)間:2025/3/21 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 程序员面试题精选100题(02)-设计包含min函数的栈[数据结构] 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目:定義棧的數(shù)據(jù)結(jié)構(gòu),要求添加一個(gè)min函數(shù),能夠得到棧的最小元素。要求函數(shù)min、push以及pop的時(shí)間復(fù)雜度都是O(1)。

分析:這是去年google的一道面試題。

我看到這道題目時(shí),第一反應(yīng)就是每次push一個(gè)新元素時(shí),將棧里所有逆序元素排序。這樣棧頂元素將是最小元素。但由于不能保證最后push進(jìn)棧的元素最先出棧,這種思路設(shè)計(jì)的數(shù)據(jù)結(jié)構(gòu)已經(jīng)不是一個(gè)棧了。

在棧里添加一個(gè)成員變量存放最小元素(或最小元素的位置)。每次push一個(gè)新元素進(jìn)棧的時(shí)候,如果該元素比當(dāng)前的最小元素還要小,則更新最小元素。

乍一看這樣思路挺好的。但仔細(xì)一想,該思路存在一個(gè)重要的問(wèn)題:如果當(dāng)前最小元素被pop出去,如何才能得到下一個(gè)最小元素?

因此僅僅只添加一個(gè)成員變量存放最小元素(或最小元素的位置)是不夠的。我們需要一個(gè)輔助棧。每次push一個(gè)新元素的時(shí)候,同時(shí)將最小元素(或最小元素的位置。考慮到棧元素的類型可能是復(fù)雜的數(shù)據(jù)結(jié)構(gòu),用最小元素的位置將能減少空間消耗)push到輔助棧中;每次pop一個(gè)元素出棧的時(shí)候,同時(shí)pop輔助棧。

參考代碼:

#include <deque> #include <assert.h>template <typename T> 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; // the elements of stacksize_t> m_minIndex; // the indices of minimum elements };// get the last element of mutable stack template <typename T> T& CStackWithMin<T>::top() {return m_data.back(); }// get the last element of non-mutable stack template <typename T> const T& CStackWithMin<T>::top() const {return m_data.back(); }// insert an elment at the end of stack template <typename T> void CStackWithMin<T>::push(const T& value) {// append the data into the end of m_datam_data.push_back(value);// set the index of minimum elment in m_data at the end of m_minIndexif(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);elsem_minIndex.push_back(m_minIndex.back());} }// erease the element at the end of stack template <typename T> void CStackWithMin<T>::pop() {// pop m_datam_data.pop_back();// pop m_minIndexm_minIndex.pop_back(); }// get the minimum element of stack template <typename T> const T& CStackWithMin<T>::min() const {assert(m_data.size() > 0);assert(m_minIndex.size() > 0);return m_data[m_minIndex.back()]; }



舉個(gè)例子演示上述代碼的運(yùn)行過(guò)程:

? 步驟????????????? 數(shù)據(jù)棧??????????? 輔助棧??????????????? 最小值
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

討論:如果思路正確,編寫上述代碼不是一件很難的事情。但如果能注意一些細(xì)節(jié)無(wú)疑能在面試中加分。比如我在上面的代碼中做了如下的工作:

·???????? 用模板類實(shí)現(xiàn)。如果別人的元素類型只是int類型,模板將能給面試官帶來(lái)好印象;

·???????? 兩個(gè)版本的top函數(shù)。在很多類中,都需要提供const和非const版本的成員訪問(wèn)函數(shù);

·???????? min函數(shù)中assert。把代碼寫的盡量安全是每個(gè)軟件公司對(duì)程序員的要求;

·???????? 添加一些注釋。注釋既能提高代碼的可讀性,又能增加代碼量,何樂(lè)而不為?

總之,在面試時(shí)如果時(shí)間允許,盡量把代碼寫的漂亮一些。說(shuō)不定代碼中的幾個(gè)小亮點(diǎn)就能讓自己輕松拿到心儀的Offer。


本文已經(jīng)收錄到《劍指Offer——名企面試官精講典型編程題》一書(shū)中,有改動(dòng),書(shū)中的分析講解更加詳細(xì)。歡迎關(guān)注。我在英文博客上提供了一種不需要輔助棧的算法。感興趣的讀者請(qǐng)參考http://codercareer.blogspot.com/2011/09/no-02-stack-with-function-min.html。

博主何海濤對(duì)本博客文章享有版權(quán)。網(wǎng)絡(luò)轉(zhuǎn)載請(qǐng)注明出處http://zhedahht.blog.163.com/。整理出版物請(qǐng)和作者聯(lián)系。對(duì)解題思路有任何建議,歡迎在評(píng)論中告知,或者加我微博http://weibo.com/zhedahht或者http://t.163.com/zhedahht與我討論。謝謝。

總結(jié)

以上是生活随笔為你收集整理的程序员面试题精选100题(02)-设计包含min函数的栈[数据结构]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。