題目描述:
乙個王國裡住著國王、他的孩子們、他的孫子們等等。每乙個時間點,這個家庭裡有人出生也有人死亡。
這個王國有乙個明確規定的皇位繼承順序,第一繼承人總是國王自己。我們定義遞迴函式 successor(x, curorder) ,給定乙個人 x 和當前的繼承順序,該函式返回 x 的下一繼承人。
successor(x, curorder):
如果 x 沒有孩子或者所有 x 的孩子都在 curorder 中:
如果 x 是國王,那麼返回 null
否則,返回 successor(x 的父親, curorder)
否則,返回 x 不在 curorder 中最年長的孩子
比方說,假設王國由國王,他的孩子 alice 和 bob (alice 比 bob 年長)和 alice 的孩子 jack 組成。
一開始, curorder 為 [「king」].
呼叫 successor(king, curorder) ,返回 alice ,所以我們將 alice 放入 curorder 中,得到 [「king」, 「alice」] 。
呼叫 successor(alice, curorder) ,返回 jack ,所以我們將 jack 放入 curorder 中,得到 [「king」, 「alice」, 「jack」] 。
呼叫 successor(jack, curorder) ,返回 bob ,所以我們將 bob 放入 curorder 中,得到 [「king」, 「alice」, 「jack」, 「bob」] 。
呼叫 successor(bob, curorder) ,返回 null 。最終得到繼承順序為 [「king」, 「alice」, 「jack」, 「bob」] 。
通過以上的函式,我們總是能得到乙個唯一的繼承順序。
請你實現 throneinheritance 類:
throneinheritance(string kingname) 初始化乙個 throneinheritance 類的物件。國王的名字作為建構函式的引數傳入。
void birth(string parentname, string childname) 表示 parentname 新擁有了乙個名為 childname 的孩子。
void death(string name) 表示名為 name 的人死亡。乙個人的死亡不會影響 successor 函式,也不會影響當前的繼承順序。你可以只將這個人標記為死亡狀態。
string getinheritanceorder() 返回 除去 死亡人員的當前繼承順序列表。
示例:
輸入:[「throneinheritance」, 「birth」, 「birth」, 「birth」, 「birth」, 「birth」, 「birth」, 「getinheritanceorder」, 「death」, 「getinheritanceorder」]
[[「king」], [「king」, 「andy」], [「king」, 「bob」], [「king」, 「catherine」], [「andy」, 「matthew」], [「bob」, 「alex」], [「bob」, 「asha」], [null], [「bob」], [null]]
輸出:[null, null, null, null, null, null, null, [「king」, 「andy」, 「matthew」, 「bob」, 「alex」, 「asha」, 「catherine」], null, [「king」, 「andy」, 「matthew」, 「alex」, 「asha」, 「catherine」]]
解釋:throneinheritance t= new throneinheritance(「king」); // 繼承順序:king
t.birth(「king」, 「andy」); // 繼承順序:king > andy
t.birth(「king」, 「bob」); // 繼承順序:king > andy > bob
t.birth(「king」, 「catherine」); // 繼承順序:king > andy > bob > catherine
t.birth(「andy」, 「matthew」); // 繼承順序:king > andy > matthew > bob > catherine
t.birth(「bob」, 「alex」); // 繼承順序:king > andy > matthew > bob > alex > catherine
t.birth(「bob」, 「asha」); // 繼承順序:king > andy > matthew > bob > alex > asha > catherine
t.getinheritanceorder(); // 返回 [「king」, 「andy」, 「matthew」, 「bob」, 「alex」, 「asha」, 「catherine」]
t.death(「bob」); // 繼承順序:king > andy > matthew > bob(已經去世)> alex > asha > catherine
t.getinheritanceorder(); // 返回 [「king」, 「andy」, 「matthew」, 「alex」, 「asha」, 「catherine」]
1 <= kingname.length, parentname.length, childname.length, name.length <= 15
kingname,parentname, childname 和 name 僅包含小寫英文本母。
所有的引數 childname 和 kingname 互不相同。
所有 death 函式中的死亡名字 name 要麼是國王,要麼是已經出生了的人員名字。
每次呼叫 birth(parentname, childname) 時,測試用例都保證 parentname 對應的人員是活著的。
最多呼叫 105 次birth 和 death 。
最多呼叫 10 次 getinheritanceorder 。
方法1:
主要思路:
(1)建立類似於鄰接表的圖,統計各個名字之間的直接關係;
(2)單獨使用乙個unordered_set 來統計死去的名字;
(3)獲得繼承順序時,直接使用深度搜尋,進行搜尋即可,只要過濾掉已經死過的名字即可;
class
throneinheritance
;//初始化
}void
birth
(string parentname, string childname)
void
death
(string name)
vector
getinheritanceorder()
void
helper
(string& cur_parent,vector
& res)
//鄰接表的深度搜尋
for(
int i=
0;i.size()
;++i)}}
;/**
* your throneinheritance object will be instantiated and called as such:
* throneinheritance* obj = new throneinheritance(kingname);
* obj->birth(parentname,childname);
* obj->death(name);
* vectorparam_3 = obj->getinheritanceorder();
*/
python繼承順序
python和c 一樣,支援多繼承。概念雖然容易,但是困難的工作是如果子類呼叫乙個自身沒有定義的屬性,它是按照何種順序去到父類尋找呢,尤其是眾多父類中有多個都包含該同名屬性。class p1 object def foo self print p1 foo class p2 object def f...
Python 繼承順序
class base def init self print base.init class a base def init self super init print a.init class b base def init self super init print b.init class c...
類的繼承順序
要背的內容 只要繼承object類就是新式類 不繼承object類的都是經典類 在python3中 所有的類都繼承object類,都是新式類經典類 在py3中不存是,在py2中不主動繼承object的類 在py2中 class a pass 經典類 class b object pass 新式類 在...