1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/王位继承顺序 [throne-inheritance].html
2022-03-29 12:43:11 +08:00

71 lines
5.2 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点,这个家庭里有人出生也有人死亡。</p>
<p>这个王国有一个明确规定的王位继承顺序,第一继承人总是国王自己。我们定义递归函数&nbsp;<code>Successor(x, curOrder)</code>&nbsp;,给定一个人&nbsp;<code>x</code>&nbsp;和当前的继承顺序,该函数返回 <code>x</code>&nbsp;的下一继承人。</p>
<pre>
Successor(x, curOrder):
如果 x 没有孩子或者所有 x 的孩子都在 curOrder 中:
如果 x 是国王,那么返回 null
否则,返回 Successor(x 的父亲, curOrder)
否则,返回 x 不在 curOrder 中最年长的孩子
</pre>
<p>比方说,假设王国由国王,他的孩子&nbsp;Alice 和 Bob Alice 比 Bob&nbsp;年长)和 Alice 的孩子&nbsp;Jack 组成。</p>
<ol>
<li>一开始,&nbsp;<code>curOrder</code>&nbsp;&nbsp;<code>["king"]</code>.</li>
<li>调用&nbsp;<code>Successor(king, curOrder)</code>&nbsp;,返回 Alice ,所以我们将 Alice 放入 <code>curOrder</code>&nbsp;中,得到&nbsp;<code>["king", "Alice"]</code>&nbsp;</li>
<li>调用&nbsp;<code>Successor(Alice, curOrder)</code>&nbsp;,返回 Jack ,所以我们将 Jack 放入&nbsp;<code>curOrder</code>&nbsp;中,得到&nbsp;<code>["king", "Alice", "Jack"]</code>&nbsp;</li>
<li>调用&nbsp;<code>Successor(Jack, curOrder)</code>&nbsp;,返回 Bob ,所以我们将 Bob 放入&nbsp;<code>curOrder</code>&nbsp;中,得到&nbsp;<code>["king", "Alice", "Jack", "Bob"]</code>&nbsp;</li>
<li>调用&nbsp;<code>Successor(Bob, curOrder)</code>&nbsp;,返回&nbsp;<code>null</code>&nbsp;。最终得到继承顺序为&nbsp;<code>["king", "Alice", "Jack", "Bob"]</code>&nbsp;</li>
</ol>
<p>通过以上的函数,我们总是能得到一个唯一的继承顺序。</p>
<p>请你实现&nbsp;<code>ThroneInheritance</code>&nbsp;类:</p>
<ul>
<li><code>ThroneInheritance(string kingName)</code> 初始化一个&nbsp;<code>ThroneInheritance</code>&nbsp;类的对象。国王的名字作为构造函数的参数传入。</li>
<li><code>void birth(string parentName, string childName)</code>&nbsp;表示&nbsp;<code>parentName</code>&nbsp;新拥有了一个名为&nbsp;<code>childName</code>&nbsp;的孩子。</li>
<li><code>void death(string name)</code>&nbsp;表示名为&nbsp;<code>name</code>&nbsp;的人死亡。一个人的死亡不会影响&nbsp;<code>Successor</code>&nbsp;函数,也不会影响当前的继承顺序。你可以只将这个人标记为死亡状态。</li>
<li><code>string[] getInheritanceOrder()</code>&nbsp;返回 <strong>除去</strong>&nbsp;死亡人员的当前继承顺序列表。</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre>
<strong>输入:</strong>
["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]]
<strong>输出:</strong>
[null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]]
<strong>解释:</strong>
ThroneInheritance t= new ThroneInheritance("king"); // 继承顺序:<strong>king</strong>
t.birth("king", "andy"); // 继承顺序king &gt; <strong>andy</strong>
t.birth("king", "bob"); // 继承顺序king &gt; andy &gt; <strong>bob</strong>
t.birth("king", "catherine"); // 继承顺序king &gt; andy &gt; bob &gt; <strong>catherine</strong>
t.birth("andy", "matthew"); // 继承顺序king &gt; andy &gt; <strong>matthew</strong> &gt; bob &gt; catherine
t.birth("bob", "alex"); // 继承顺序king &gt; andy &gt; matthew &gt; bob &gt; <strong>alex</strong> &gt; catherine
t.birth("bob", "asha"); // 继承顺序king &gt; andy &gt; matthew &gt; bob &gt; alex &gt; <strong>asha</strong> &gt; catherine
t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
t.death("bob"); // 继承顺序king &gt; andy &gt; matthew &gt; <strong>bob已经去世</strong>&gt; alex &gt; asha &gt; catherine
t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "alex", "asha", "catherine"]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= kingName.length, parentName.length, childName.length, name.length &lt;= 15</code></li>
<li><code>kingName</code><code>parentName</code>&nbsp;<code>childName</code>&nbsp;&nbsp;<code>name</code>&nbsp;仅包含小写英文字母。</li>
<li>所有的参数&nbsp;<code>childName</code>&nbsp;<code>kingName</code>&nbsp;<strong>互不相同</strong></li>
<li>所有&nbsp;<code>death</code>&nbsp;函数中的死亡名字 <code>name</code>&nbsp;要么是国王,要么是已经出生了的人员名字。</li>
<li>每次调用 <code>birth(parentName, childName)</code> 时,测试用例都保证 <code>parentName</code> 对应的人员是活着的。</li>
<li>最多调用&nbsp;<code>10<sup>5</sup></code>&nbsp;<code>birth</code>&nbsp;<code>death</code>&nbsp;</li>
<li>最多调用&nbsp;<code>10</code>&nbsp;&nbsp;<code>getInheritanceOrder</code>&nbsp;</li>
</ul>