mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-11 06:56:06 +00:00
deploy
This commit is contained in:
@@ -1917,7 +1917,8 @@
|
||||
</div>
|
||||
<h2 id="1221">12.2.1. 无重复的情况<a class="headerlink" href="#1221" title="Permanent link">¶</a></h2>
|
||||
<div class="admonition question">
|
||||
<p class="admonition-title">输入一个整数数组,数组中不包含重复元素,返回所有可能的排列。</p>
|
||||
<p class="admonition-title">Question</p>
|
||||
<p>输入一个整数数组,数组中不包含重复元素,返回所有可能的排列。</p>
|
||||
</div>
|
||||
<p><strong>从回溯算法的角度看,我们可以把生成排列的过程想象成一系列选择的结果</strong>。假设输入数组为 <span class="arithmatex">\([1, 2, 3]\)</span> ,如果我们先选择 <span class="arithmatex">\(1\)</span> 、再选择 <span class="arithmatex">\(3\)</span> 、最后选择 <span class="arithmatex">\(2\)</span> ,则获得排列 <span class="arithmatex">\([1, 3, 2]\)</span> 。回退表示撤销一个选择,之后继续尝试其他选择。</p>
|
||||
<p>从回溯算法代码的角度看,候选集合 <code>choices</code> 是输入数组中的所有元素,状态 <code>state</code> 是直至目前已被选择的元素。注意,每个元素只允许被选择一次,<strong>因此在遍历选择时,应当排除已经选择过的元素</strong>。</p>
|
||||
@@ -2217,7 +2218,8 @@
|
||||
|
||||
<h2 id="1222">12.2.2. 考虑重复的情况<a class="headerlink" href="#1222" title="Permanent link">¶</a></h2>
|
||||
<div class="admonition question">
|
||||
<p class="admonition-title">输入一个整数数组,<strong>数组中可能包含重复元素</strong>,返回所有不重复的排列。</p>
|
||||
<p class="admonition-title">Question</p>
|
||||
<p>输入一个整数数组,<strong>数组中可能包含重复元素</strong>,返回所有不重复的排列。</p>
|
||||
</div>
|
||||
<p>假设输入数组为 <span class="arithmatex">\([1, 1, 2]\)</span> 。为了方便区分两个重复的元素 <span class="arithmatex">\(1\)</span> ,接下来我们将第二个元素记为 <span class="arithmatex">\(\hat{1}\)</span> 。如下图所示,上述方法生成的排列有一半都是重复的。</p>
|
||||
<p><img alt="重复排列" src="../permutations_problem.assets/permutations_ii.png" /></p>
|
||||
|
||||
Reference in New Issue
Block a user