This commit is contained in:
krahets 2023-05-04 05:32:16 +08:00
parent 73dc37df52
commit d09a5e81f3
4 changed files with 71 additions and 13 deletions

View file

@ -1980,10 +1980,10 @@
<a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a> <span class="n">row</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a> <span class="n">row</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
<a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a> <span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a> <span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
<a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a> <span class="n">state</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">list</span><span class="p">[</span><span class="nb">str</span><span class="p">]],</span> <a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a> <span class="n">state</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">list</span><span class="p">[</span><span class="nb">str</span><span class="p">]],</span>
<a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a> <span class="n">cols</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">bool</span><span class="p">],</span> <a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a> <span class="n">res</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">list</span><span class="p">[</span><span class="nb">list</span><span class="p">[</span><span class="nb">str</span><span class="p">]]],</span>
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a> <span class="n">diags1</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">bool</span><span class="p">],</span> <a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a> <span class="n">cols</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">bool</span><span class="p">],</span>
<a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a> <span class="n">diags2</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">bool</span><span class="p">],</span> <a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a> <span class="n">diags1</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">bool</span><span class="p">],</span>
<a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a> <span class="n">res</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">list</span><span class="p">[</span><span class="nb">list</span><span class="p">[</span><span class="nb">str</span><span class="p">]]],</span> <a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a> <span class="n">diags2</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">bool</span><span class="p">],</span>
<a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a><span class="p">):</span> <a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a><span class="p">):</span>
<a id="__codelineno-2-10" name="__codelineno-2-10" href="#__codelineno-2-10"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;回溯算法N 皇后&quot;&quot;&quot;</span> <a id="__codelineno-2-10" name="__codelineno-2-10" href="#__codelineno-2-10"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;回溯算法N 皇后&quot;&quot;&quot;</span>
<a id="__codelineno-2-11" name="__codelineno-2-11" href="#__codelineno-2-11"></a> <span class="c1"># 当放置完所有行时,记录解</span> <a id="__codelineno-2-11" name="__codelineno-2-11" href="#__codelineno-2-11"></a> <span class="c1"># 当放置完所有行时,记录解</span>
@ -2001,7 +2001,7 @@
<a id="__codelineno-2-23" name="__codelineno-2-23" href="#__codelineno-2-23"></a> <span class="n">state</span><span class="p">[</span><span class="n">row</span><span class="p">][</span><span class="n">col</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;Q&quot;</span> <a id="__codelineno-2-23" name="__codelineno-2-23" href="#__codelineno-2-23"></a> <span class="n">state</span><span class="p">[</span><span class="n">row</span><span class="p">][</span><span class="n">col</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;Q&quot;</span>
<a id="__codelineno-2-24" name="__codelineno-2-24" href="#__codelineno-2-24"></a> <span class="n">cols</span><span class="p">[</span><span class="n">col</span><span class="p">]</span> <span class="o">=</span> <span class="n">diags1</span><span class="p">[</span><span class="n">diag1</span><span class="p">]</span> <span class="o">=</span> <span class="n">diags2</span><span class="p">[</span><span class="n">diag2</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span> <a id="__codelineno-2-24" name="__codelineno-2-24" href="#__codelineno-2-24"></a> <span class="n">cols</span><span class="p">[</span><span class="n">col</span><span class="p">]</span> <span class="o">=</span> <span class="n">diags1</span><span class="p">[</span><span class="n">diag1</span><span class="p">]</span> <span class="o">=</span> <span class="n">diags2</span><span class="p">[</span><span class="n">diag2</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<a id="__codelineno-2-25" name="__codelineno-2-25" href="#__codelineno-2-25"></a> <span class="c1"># 放置下一行</span> <a id="__codelineno-2-25" name="__codelineno-2-25" href="#__codelineno-2-25"></a> <span class="c1"># 放置下一行</span>
<a id="__codelineno-2-26" name="__codelineno-2-26" href="#__codelineno-2-26"></a> <span class="n">backtrack</span><span class="p">(</span><span class="n">row</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">state</span><span class="p">,</span> <span class="n">cols</span><span class="p">,</span> <span class="n">diags1</span><span class="p">,</span> <span class="n">diags2</span><span class="p">,</span> <span class="n">res</span><span class="p">)</span> <a id="__codelineno-2-26" name="__codelineno-2-26" href="#__codelineno-2-26"></a> <span class="n">backtrack</span><span class="p">(</span><span class="n">row</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">state</span><span class="p">,</span> <span class="n">res</span><span class="p">,</span> <span class="n">cols</span><span class="p">,</span> <span class="n">diags1</span><span class="p">,</span> <span class="n">diags2</span><span class="p">)</span>
<a id="__codelineno-2-27" name="__codelineno-2-27" href="#__codelineno-2-27"></a> <span class="c1"># 回退:将该格子恢复为空位</span> <a id="__codelineno-2-27" name="__codelineno-2-27" href="#__codelineno-2-27"></a> <span class="c1"># 回退:将该格子恢复为空位</span>
<a id="__codelineno-2-28" name="__codelineno-2-28" href="#__codelineno-2-28"></a> <span class="n">state</span><span class="p">[</span><span class="n">row</span><span class="p">][</span><span class="n">col</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;#&quot;</span> <a id="__codelineno-2-28" name="__codelineno-2-28" href="#__codelineno-2-28"></a> <span class="n">state</span><span class="p">[</span><span class="n">row</span><span class="p">][</span><span class="n">col</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;#&quot;</span>
<a id="__codelineno-2-29" name="__codelineno-2-29" href="#__codelineno-2-29"></a> <span class="n">cols</span><span class="p">[</span><span class="n">col</span><span class="p">]</span> <span class="o">=</span> <span class="n">diags1</span><span class="p">[</span><span class="n">diag1</span><span class="p">]</span> <span class="o">=</span> <span class="n">diags2</span><span class="p">[</span><span class="n">diag2</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span> <a id="__codelineno-2-29" name="__codelineno-2-29" href="#__codelineno-2-29"></a> <span class="n">cols</span><span class="p">[</span><span class="n">col</span><span class="p">]</span> <span class="o">=</span> <span class="n">diags1</span><span class="p">[</span><span class="n">diag1</span><span class="p">]</span> <span class="o">=</span> <span class="n">diags2</span><span class="p">[</span><span class="n">diag2</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
@ -2014,7 +2014,7 @@
<a id="__codelineno-2-36" name="__codelineno-2-36" href="#__codelineno-2-36"></a> <span class="n">diags1</span> <span class="o">=</span> <span class="p">[</span><span class="kc">False</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># 记录主对角线是否有皇后</span> <a id="__codelineno-2-36" name="__codelineno-2-36" href="#__codelineno-2-36"></a> <span class="n">diags1</span> <span class="o">=</span> <span class="p">[</span><span class="kc">False</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># 记录主对角线是否有皇后</span>
<a id="__codelineno-2-37" name="__codelineno-2-37" href="#__codelineno-2-37"></a> <span class="n">diags2</span> <span class="o">=</span> <span class="p">[</span><span class="kc">False</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># 记录副对角线是否有皇后</span> <a id="__codelineno-2-37" name="__codelineno-2-37" href="#__codelineno-2-37"></a> <span class="n">diags2</span> <span class="o">=</span> <span class="p">[</span><span class="kc">False</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># 记录副对角线是否有皇后</span>
<a id="__codelineno-2-38" name="__codelineno-2-38" href="#__codelineno-2-38"></a> <span class="n">res</span> <span class="o">=</span> <span class="p">[]</span> <a id="__codelineno-2-38" name="__codelineno-2-38" href="#__codelineno-2-38"></a> <span class="n">res</span> <span class="o">=</span> <span class="p">[]</span>
<a id="__codelineno-2-39" name="__codelineno-2-39" href="#__codelineno-2-39"></a> <span class="n">backtrack</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">state</span><span class="p">,</span> <span class="n">cols</span><span class="p">,</span> <span class="n">diags1</span><span class="p">,</span> <span class="n">diags2</span><span class="p">,</span> <span class="n">res</span><span class="p">)</span> <a id="__codelineno-2-39" name="__codelineno-2-39" href="#__codelineno-2-39"></a> <span class="n">backtrack</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">state</span><span class="p">,</span> <span class="n">res</span><span class="p">,</span> <span class="n">cols</span><span class="p">,</span> <span class="n">diags1</span><span class="p">,</span> <span class="n">diags2</span><span class="p">)</span>
<a id="__codelineno-2-40" name="__codelineno-2-40" href="#__codelineno-2-40"></a> <a id="__codelineno-2-40" name="__codelineno-2-40" href="#__codelineno-2-40"></a>
<a id="__codelineno-2-41" name="__codelineno-2-41" href="#__codelineno-2-41"></a> <span class="k">return</span> <span class="n">res</span> <a id="__codelineno-2-41" name="__codelineno-2-41" href="#__codelineno-2-41"></a> <span class="k">return</span> <span class="n">res</span>
</code></pre></div> </code></pre></div>

View file

@ -2074,9 +2074,37 @@
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
<div class="highlight"><span class="filename">permutations_i.swift</span><pre><span></span><code><a id="__codelineno-8-1" name="__codelineno-8-1" href="#__codelineno-8-1"></a><span class="p">[</span><span class="kd">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="kd">func</span><span class="p">]{</span><span class="n">backtrack</span><span class="p">}</span> <div class="highlight"><span class="filename">permutations_i.swift</span><pre><span></span><code><a id="__codelineno-8-1" name="__codelineno-8-1" href="#__codelineno-8-1"></a><span class="cm">/* 回溯算法:全排列 I */</span>
<a id="__codelineno-8-2" name="__codelineno-8-2" href="#__codelineno-8-2"></a> <a id="__codelineno-8-2" name="__codelineno-8-2" href="#__codelineno-8-2"></a><span class="kd">func</span> <span class="nf">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="kr">inout</span> <span class="p">[</span><span class="nb">Int</span><span class="p">],</span> <span class="n">choices</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">],</span> <span class="n">selected</span><span class="p">:</span> <span class="kr">inout</span> <span class="p">[</span><span class="nb">Bool</span><span class="p">],</span> <span class="n">res</span><span class="p">:</span> <span class="kr">inout</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]])</span> <span class="p">{</span>
<a id="__codelineno-8-3" name="__codelineno-8-3" href="#__codelineno-8-3"></a><span class="p">[</span><span class="kd">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="kd">func</span><span class="p">]{</span><span class="n">permutationsI</span><span class="p">}</span> <a id="__codelineno-8-3" name="__codelineno-8-3" href="#__codelineno-8-3"></a> <span class="c1">// 当状态长度等于元素数量时,记录解</span>
<a id="__codelineno-8-4" name="__codelineno-8-4" href="#__codelineno-8-4"></a> <span class="k">if</span> <span class="n">state</span><span class="p">.</span><span class="bp">count</span> <span class="p">==</span> <span class="n">choices</span><span class="p">.</span><span class="bp">count</span> <span class="p">{</span>
<a id="__codelineno-8-5" name="__codelineno-8-5" href="#__codelineno-8-5"></a> <span class="n">res</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">state</span><span class="p">)</span>
<a id="__codelineno-8-6" name="__codelineno-8-6" href="#__codelineno-8-6"></a> <span class="k">return</span>
<a id="__codelineno-8-7" name="__codelineno-8-7" href="#__codelineno-8-7"></a> <span class="p">}</span>
<a id="__codelineno-8-8" name="__codelineno-8-8" href="#__codelineno-8-8"></a> <span class="c1">// 遍历所有选择</span>
<a id="__codelineno-8-9" name="__codelineno-8-9" href="#__codelineno-8-9"></a> <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">choice</span><span class="p">)</span> <span class="k">in</span> <span class="n">choices</span><span class="p">.</span><span class="n">enumerated</span><span class="p">()</span> <span class="p">{</span>
<a id="__codelineno-8-10" name="__codelineno-8-10" href="#__codelineno-8-10"></a> <span class="c1">// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素</span>
<a id="__codelineno-8-11" name="__codelineno-8-11" href="#__codelineno-8-11"></a> <span class="k">if</span> <span class="o">!</span><span class="n">selected</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="p">{</span>
<a id="__codelineno-8-12" name="__codelineno-8-12" href="#__codelineno-8-12"></a> <span class="c1">// 尝试:做出选择,更新状态</span>
<a id="__codelineno-8-13" name="__codelineno-8-13" href="#__codelineno-8-13"></a> <span class="n">selected</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="p">=</span> <span class="kc">true</span>
<a id="__codelineno-8-14" name="__codelineno-8-14" href="#__codelineno-8-14"></a> <span class="n">state</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">choice</span><span class="p">)</span>
<a id="__codelineno-8-15" name="__codelineno-8-15" href="#__codelineno-8-15"></a> <span class="c1">// 进行下一轮选择</span>
<a id="__codelineno-8-16" name="__codelineno-8-16" href="#__codelineno-8-16"></a> <span class="n">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">state</span><span class="p">,</span> <span class="n">choices</span><span class="p">:</span> <span class="n">choices</span><span class="p">,</span> <span class="n">selected</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">selected</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">res</span><span class="p">)</span>
<a id="__codelineno-8-17" name="__codelineno-8-17" href="#__codelineno-8-17"></a> <span class="c1">// 回退:撤销选择,恢复到之前的状态</span>
<a id="__codelineno-8-18" name="__codelineno-8-18" href="#__codelineno-8-18"></a> <span class="n">selected</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="p">=</span> <span class="kc">false</span>
<a id="__codelineno-8-19" name="__codelineno-8-19" href="#__codelineno-8-19"></a> <span class="n">state</span><span class="p">.</span><span class="bp">removeLast</span><span class="p">()</span>
<a id="__codelineno-8-20" name="__codelineno-8-20" href="#__codelineno-8-20"></a> <span class="p">}</span>
<a id="__codelineno-8-21" name="__codelineno-8-21" href="#__codelineno-8-21"></a> <span class="p">}</span>
<a id="__codelineno-8-22" name="__codelineno-8-22" href="#__codelineno-8-22"></a><span class="p">}</span>
<a id="__codelineno-8-23" name="__codelineno-8-23" href="#__codelineno-8-23"></a>
<a id="__codelineno-8-24" name="__codelineno-8-24" href="#__codelineno-8-24"></a><span class="cm">/* 全排列 I */</span>
<a id="__codelineno-8-25" name="__codelineno-8-25" href="#__codelineno-8-25"></a><span class="kd">func</span> <span class="nf">permutationsI</span><span class="p">(</span><span class="n">nums</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">])</span> <span class="p">-&gt;</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]]</span> <span class="p">{</span>
<a id="__codelineno-8-26" name="__codelineno-8-26" href="#__codelineno-8-26"></a> <span class="kd">var</span> <span class="nv">state</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">]</span> <span class="p">=</span> <span class="p">[]</span>
<a id="__codelineno-8-27" name="__codelineno-8-27" href="#__codelineno-8-27"></a> <span class="kd">var</span> <span class="nv">selected</span> <span class="p">=</span> <span class="nb">Array</span><span class="p">(</span><span class="n">repeating</span><span class="p">:</span> <span class="kc">false</span><span class="p">,</span> <span class="bp">count</span><span class="p">:</span> <span class="n">nums</span><span class="p">.</span><span class="bp">count</span><span class="p">)</span>
<a id="__codelineno-8-28" name="__codelineno-8-28" href="#__codelineno-8-28"></a> <span class="kd">var</span> <span class="nv">res</span><span class="p">:</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]]</span> <span class="p">=</span> <span class="p">[]</span>
<a id="__codelineno-8-29" name="__codelineno-8-29" href="#__codelineno-8-29"></a> <span class="n">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">state</span><span class="p">,</span> <span class="n">choices</span><span class="p">:</span> <span class="n">nums</span><span class="p">,</span> <span class="n">selected</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">selected</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">res</span><span class="p">)</span>
<a id="__codelineno-8-30" name="__codelineno-8-30" href="#__codelineno-8-30"></a> <span class="k">return</span> <span class="n">res</span>
<a id="__codelineno-8-31" name="__codelineno-8-31" href="#__codelineno-8-31"></a><span class="p">}</span>
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
@ -2271,9 +2299,39 @@
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
<div class="highlight"><span class="filename">permutations_ii.swift</span><pre><span></span><code><a id="__codelineno-18-1" name="__codelineno-18-1" href="#__codelineno-18-1"></a><span class="p">[</span><span class="kd">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="kd">func</span><span class="p">]{</span><span class="n">backtrack</span><span class="p">}</span> <div class="highlight"><span class="filename">permutations_ii.swift</span><pre><span></span><code><a id="__codelineno-18-1" name="__codelineno-18-1" href="#__codelineno-18-1"></a><span class="cm">/* 回溯算法:全排列 II */</span>
<a id="__codelineno-18-2" name="__codelineno-18-2" href="#__codelineno-18-2"></a> <a id="__codelineno-18-2" name="__codelineno-18-2" href="#__codelineno-18-2"></a><span class="kd">func</span> <span class="nf">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="kr">inout</span> <span class="p">[</span><span class="nb">Int</span><span class="p">],</span> <span class="n">choices</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">],</span> <span class="n">selected</span><span class="p">:</span> <span class="kr">inout</span> <span class="p">[</span><span class="nb">Bool</span><span class="p">],</span> <span class="n">res</span><span class="p">:</span> <span class="kr">inout</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]])</span> <span class="p">{</span>
<a id="__codelineno-18-3" name="__codelineno-18-3" href="#__codelineno-18-3"></a><span class="p">[</span><span class="kd">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="kd">func</span><span class="p">]{</span><span class="n">permutationsII</span><span class="p">}</span> <a id="__codelineno-18-3" name="__codelineno-18-3" href="#__codelineno-18-3"></a> <span class="c1">// 当状态长度等于元素数量时,记录解</span>
<a id="__codelineno-18-4" name="__codelineno-18-4" href="#__codelineno-18-4"></a> <span class="k">if</span> <span class="n">state</span><span class="p">.</span><span class="bp">count</span> <span class="p">==</span> <span class="n">choices</span><span class="p">.</span><span class="bp">count</span> <span class="p">{</span>
<a id="__codelineno-18-5" name="__codelineno-18-5" href="#__codelineno-18-5"></a> <span class="n">res</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">state</span><span class="p">)</span>
<a id="__codelineno-18-6" name="__codelineno-18-6" href="#__codelineno-18-6"></a> <span class="k">return</span>
<a id="__codelineno-18-7" name="__codelineno-18-7" href="#__codelineno-18-7"></a> <span class="p">}</span>
<a id="__codelineno-18-8" name="__codelineno-18-8" href="#__codelineno-18-8"></a> <span class="c1">// 遍历所有选择</span>
<a id="__codelineno-18-9" name="__codelineno-18-9" href="#__codelineno-18-9"></a> <span class="kd">var</span> <span class="nv">duplicated</span><span class="p">:</span> <span class="n">Set</span><span class="p">&lt;</span><span class="nb">Int</span><span class="p">&gt;</span> <span class="p">=</span> <span class="p">[]</span>
<a id="__codelineno-18-10" name="__codelineno-18-10" href="#__codelineno-18-10"></a> <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">choice</span><span class="p">)</span> <span class="k">in</span> <span class="n">choices</span><span class="p">.</span><span class="n">enumerated</span><span class="p">()</span> <span class="p">{</span>
<a id="__codelineno-18-11" name="__codelineno-18-11" href="#__codelineno-18-11"></a> <span class="c1">// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素</span>
<a id="__codelineno-18-12" name="__codelineno-18-12" href="#__codelineno-18-12"></a> <span class="k">if</span> <span class="o">!</span><span class="n">selected</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="o">!</span><span class="n">duplicated</span><span class="p">.</span><span class="bp">contains</span><span class="p">(</span><span class="n">choice</span><span class="p">)</span> <span class="p">{</span>
<a id="__codelineno-18-13" name="__codelineno-18-13" href="#__codelineno-18-13"></a> <span class="c1">// 尝试:做出选择,更新状态</span>
<a id="__codelineno-18-14" name="__codelineno-18-14" href="#__codelineno-18-14"></a> <span class="n">duplicated</span><span class="p">.</span><span class="bp">insert</span><span class="p">(</span><span class="n">choice</span><span class="p">)</span> <span class="c1">// 记录选择过的元素值</span>
<a id="__codelineno-18-15" name="__codelineno-18-15" href="#__codelineno-18-15"></a> <span class="n">selected</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="p">=</span> <span class="kc">true</span>
<a id="__codelineno-18-16" name="__codelineno-18-16" href="#__codelineno-18-16"></a> <span class="n">state</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">choice</span><span class="p">)</span>
<a id="__codelineno-18-17" name="__codelineno-18-17" href="#__codelineno-18-17"></a> <span class="c1">// 进行下一轮选择</span>
<a id="__codelineno-18-18" name="__codelineno-18-18" href="#__codelineno-18-18"></a> <span class="n">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">state</span><span class="p">,</span> <span class="n">choices</span><span class="p">:</span> <span class="n">choices</span><span class="p">,</span> <span class="n">selected</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">selected</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">res</span><span class="p">)</span>
<a id="__codelineno-18-19" name="__codelineno-18-19" href="#__codelineno-18-19"></a> <span class="c1">// 回退:撤销选择,恢复到之前的状态</span>
<a id="__codelineno-18-20" name="__codelineno-18-20" href="#__codelineno-18-20"></a> <span class="n">selected</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="p">=</span> <span class="kc">false</span>
<a id="__codelineno-18-21" name="__codelineno-18-21" href="#__codelineno-18-21"></a> <span class="n">state</span><span class="p">.</span><span class="bp">removeLast</span><span class="p">()</span>
<a id="__codelineno-18-22" name="__codelineno-18-22" href="#__codelineno-18-22"></a> <span class="p">}</span>
<a id="__codelineno-18-23" name="__codelineno-18-23" href="#__codelineno-18-23"></a> <span class="p">}</span>
<a id="__codelineno-18-24" name="__codelineno-18-24" href="#__codelineno-18-24"></a><span class="p">}</span>
<a id="__codelineno-18-25" name="__codelineno-18-25" href="#__codelineno-18-25"></a>
<a id="__codelineno-18-26" name="__codelineno-18-26" href="#__codelineno-18-26"></a><span class="cm">/* 全排列 II */</span>
<a id="__codelineno-18-27" name="__codelineno-18-27" href="#__codelineno-18-27"></a><span class="kd">func</span> <span class="nf">permutationsII</span><span class="p">(</span><span class="n">nums</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">])</span> <span class="p">-&gt;</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]]</span> <span class="p">{</span>
<a id="__codelineno-18-28" name="__codelineno-18-28" href="#__codelineno-18-28"></a> <span class="kd">var</span> <span class="nv">state</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">]</span> <span class="p">=</span> <span class="p">[]</span>
<a id="__codelineno-18-29" name="__codelineno-18-29" href="#__codelineno-18-29"></a> <span class="kd">var</span> <span class="nv">selected</span> <span class="p">=</span> <span class="nb">Array</span><span class="p">(</span><span class="n">repeating</span><span class="p">:</span> <span class="kc">false</span><span class="p">,</span> <span class="bp">count</span><span class="p">:</span> <span class="n">nums</span><span class="p">.</span><span class="bp">count</span><span class="p">)</span>
<a id="__codelineno-18-30" name="__codelineno-18-30" href="#__codelineno-18-30"></a> <span class="kd">var</span> <span class="nv">res</span><span class="p">:</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]]</span> <span class="p">=</span> <span class="p">[]</span>
<a id="__codelineno-18-31" name="__codelineno-18-31" href="#__codelineno-18-31"></a> <span class="n">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">state</span><span class="p">,</span> <span class="n">choices</span><span class="p">:</span> <span class="n">nums</span><span class="p">,</span> <span class="n">selected</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">selected</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">res</span><span class="p">)</span>
<a id="__codelineno-18-32" name="__codelineno-18-32" href="#__codelineno-18-32"></a> <span class="k">return</span> <span class="n">res</span>
<a id="__codelineno-18-33" name="__codelineno-18-33" href="#__codelineno-18-33"></a><span class="p">}</span>
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">

File diff suppressed because one or more lines are too long

Binary file not shown.