This commit is contained in:
krahets 2023-02-21 21:54:12 +08:00
parent bb19b1d949
commit 2877e05d84
5 changed files with 111 additions and 105 deletions

View file

@ -2576,8 +2576,8 @@
<a id="__codelineno-14-26" name="__codelineno-14-26" href="#__codelineno-14-26"></a><span class="w"> </span><span class="k">throw</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="ne">Error</span><span class="p">(</span><span class="s2">&quot;Illegal Argument Exception&quot;</span><span class="p">);</span> <a id="__codelineno-14-26" name="__codelineno-14-26" href="#__codelineno-14-26"></a><span class="w"> </span><span class="k">throw</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="ne">Error</span><span class="p">(</span><span class="s2">&quot;Illegal Argument Exception&quot;</span><span class="p">);</span>
<a id="__codelineno-14-27" name="__codelineno-14-27" href="#__codelineno-14-27"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-27" name="__codelineno-14-27" href="#__codelineno-14-27"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-28" name="__codelineno-14-28" href="#__codelineno-14-28"></a><span class="w"> </span><span class="c1">// 添加边 vet1 - vet2</span> <a id="__codelineno-14-28" name="__codelineno-14-28" href="#__codelineno-14-28"></a><span class="w"> </span><span class="c1">// 添加边 vet1 - vet2</span>
<a id="__codelineno-14-29" name="__codelineno-14-29" href="#__codelineno-14-29"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet1</span><span class="p">).</span><span class="nx">add</span><span class="p">(</span><span class="nx">vet2</span><span class="p">);</span> <a id="__codelineno-14-29" name="__codelineno-14-29" href="#__codelineno-14-29"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet1</span><span class="p">).</span><span class="nx">push</span><span class="p">(</span><span class="nx">vet2</span><span class="p">);</span>
<a id="__codelineno-14-30" name="__codelineno-14-30" href="#__codelineno-14-30"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet2</span><span class="p">).</span><span class="nx">add</span><span class="p">(</span><span class="nx">vet1</span><span class="p">);</span> <a id="__codelineno-14-30" name="__codelineno-14-30" href="#__codelineno-14-30"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet2</span><span class="p">).</span><span class="nx">push</span><span class="p">(</span><span class="nx">vet1</span><span class="p">);</span>
<a id="__codelineno-14-31" name="__codelineno-14-31" href="#__codelineno-14-31"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-31" name="__codelineno-14-31" href="#__codelineno-14-31"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-32" name="__codelineno-14-32" href="#__codelineno-14-32"></a> <a id="__codelineno-14-32" name="__codelineno-14-32" href="#__codelineno-14-32"></a>
<a id="__codelineno-14-33" name="__codelineno-14-33" href="#__codelineno-14-33"></a><span class="w"> </span><span class="cm">/* 删除边 */</span> <a id="__codelineno-14-33" name="__codelineno-14-33" href="#__codelineno-14-33"></a><span class="w"> </span><span class="cm">/* 删除边 */</span>
@ -2586,15 +2586,15 @@
<a id="__codelineno-14-36" name="__codelineno-14-36" href="#__codelineno-14-36"></a><span class="w"> </span><span class="k">throw</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="ne">Error</span><span class="p">(</span><span class="s2">&quot;Illegal Argument Exception&quot;</span><span class="p">);</span> <a id="__codelineno-14-36" name="__codelineno-14-36" href="#__codelineno-14-36"></a><span class="w"> </span><span class="k">throw</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="ne">Error</span><span class="p">(</span><span class="s2">&quot;Illegal Argument Exception&quot;</span><span class="p">);</span>
<a id="__codelineno-14-37" name="__codelineno-14-37" href="#__codelineno-14-37"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-37" name="__codelineno-14-37" href="#__codelineno-14-37"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-38" name="__codelineno-14-38" href="#__codelineno-14-38"></a><span class="w"> </span><span class="c1">// 删除边 vet1 - vet2</span> <a id="__codelineno-14-38" name="__codelineno-14-38" href="#__codelineno-14-38"></a><span class="w"> </span><span class="c1">// 删除边 vet1 - vet2</span>
<a id="__codelineno-14-39" name="__codelineno-14-39" href="#__codelineno-14-39"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet1</span><span class="p">).</span><span class="ow">delete</span><span class="p">(</span><span class="nx">vet2</span><span class="p">);</span> <a id="__codelineno-14-39" name="__codelineno-14-39" href="#__codelineno-14-39"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet1</span><span class="p">).</span><span class="nx">splice</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet1</span><span class="p">).</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">vet2</span><span class="p">),</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<a id="__codelineno-14-40" name="__codelineno-14-40" href="#__codelineno-14-40"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet2</span><span class="p">).</span><span class="ow">delete</span><span class="p">(</span><span class="nx">vet1</span><span class="p">);</span> <a id="__codelineno-14-40" name="__codelineno-14-40" href="#__codelineno-14-40"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet2</span><span class="p">).</span><span class="nx">splice</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet2</span><span class="p">).</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">vet1</span><span class="p">),</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<a id="__codelineno-14-41" name="__codelineno-14-41" href="#__codelineno-14-41"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-41" name="__codelineno-14-41" href="#__codelineno-14-41"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-42" name="__codelineno-14-42" href="#__codelineno-14-42"></a> <a id="__codelineno-14-42" name="__codelineno-14-42" href="#__codelineno-14-42"></a>
<a id="__codelineno-14-43" name="__codelineno-14-43" href="#__codelineno-14-43"></a><span class="w"> </span><span class="cm">/* 添加顶点 */</span> <a id="__codelineno-14-43" name="__codelineno-14-43" href="#__codelineno-14-43"></a><span class="w"> </span><span class="cm">/* 添加顶点 */</span>
<a id="__codelineno-14-44" name="__codelineno-14-44" href="#__codelineno-14-44"></a><span class="w"> </span><span class="nx">addVertex</span><span class="p">(</span><span class="nx">vet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-14-44" name="__codelineno-14-44" href="#__codelineno-14-44"></a><span class="w"> </span><span class="nx">addVertex</span><span class="p">(</span><span class="nx">vet</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-45" name="__codelineno-14-45" href="#__codelineno-14-45"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="nx">vet</span><span class="p">))</span><span class="w"> </span><span class="k">return</span><span class="p">;</span> <a id="__codelineno-14-45" name="__codelineno-14-45" href="#__codelineno-14-45"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="nx">vet</span><span class="p">))</span><span class="w"> </span><span class="k">return</span><span class="p">;</span>
<a id="__codelineno-14-46" name="__codelineno-14-46" href="#__codelineno-14-46"></a><span class="w"> </span><span class="c1">// 在邻接表中添加一个新链表</span> <a id="__codelineno-14-46" name="__codelineno-14-46" href="#__codelineno-14-46"></a><span class="w"> </span><span class="c1">// 在邻接表中添加一个新链表</span>
<a id="__codelineno-14-47" name="__codelineno-14-47" href="#__codelineno-14-47"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">vet</span><span class="p">,</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">());</span> <a id="__codelineno-14-47" name="__codelineno-14-47" href="#__codelineno-14-47"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">vet</span><span class="p">,</span><span class="w"> </span><span class="p">[]);</span>
<a id="__codelineno-14-48" name="__codelineno-14-48" href="#__codelineno-14-48"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-48" name="__codelineno-14-48" href="#__codelineno-14-48"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-49" name="__codelineno-14-49" href="#__codelineno-14-49"></a> <a id="__codelineno-14-49" name="__codelineno-14-49" href="#__codelineno-14-49"></a>
<a id="__codelineno-14-50" name="__codelineno-14-50" href="#__codelineno-14-50"></a><span class="w"> </span><span class="cm">/* 删除顶点 */</span> <a id="__codelineno-14-50" name="__codelineno-14-50" href="#__codelineno-14-50"></a><span class="w"> </span><span class="cm">/* 删除顶点 */</span>
@ -2606,22 +2606,25 @@
<a id="__codelineno-14-56" name="__codelineno-14-56" href="#__codelineno-14-56"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="ow">delete</span><span class="p">(</span><span class="nx">vet</span><span class="p">);</span> <a id="__codelineno-14-56" name="__codelineno-14-56" href="#__codelineno-14-56"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="ow">delete</span><span class="p">(</span><span class="nx">vet</span><span class="p">);</span>
<a id="__codelineno-14-57" name="__codelineno-14-57" href="#__codelineno-14-57"></a><span class="w"> </span><span class="c1">// 遍历其它顶点的链表,删除所有包含 vet 的边</span> <a id="__codelineno-14-57" name="__codelineno-14-57" href="#__codelineno-14-57"></a><span class="w"> </span><span class="c1">// 遍历其它顶点的链表,删除所有包含 vet 的边</span>
<a id="__codelineno-14-58" name="__codelineno-14-58" href="#__codelineno-14-58"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">set</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">values</span><span class="p">())</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-14-58" name="__codelineno-14-58" href="#__codelineno-14-58"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">set</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">values</span><span class="p">())</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-59" name="__codelineno-14-59" href="#__codelineno-14-59"></a><span class="w"> </span><span class="nx">set</span><span class="p">.</span><span class="ow">delete</span><span class="p">(</span><span class="nx">vet</span><span class="p">);</span> <a id="__codelineno-14-59" name="__codelineno-14-59" href="#__codelineno-14-59"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">set</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">vet</span><span class="p">);</span>
<a id="__codelineno-14-60" name="__codelineno-14-60" href="#__codelineno-14-60"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-60" name="__codelineno-14-60" href="#__codelineno-14-60"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">index</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="o">-</span><span class="mf">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-61" name="__codelineno-14-61" href="#__codelineno-14-61"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-61" name="__codelineno-14-61" href="#__codelineno-14-61"></a><span class="w"> </span><span class="nx">set</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<a id="__codelineno-14-62" name="__codelineno-14-62" href="#__codelineno-14-62"></a> <a id="__codelineno-14-62" name="__codelineno-14-62" href="#__codelineno-14-62"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-63" name="__codelineno-14-63" href="#__codelineno-14-63"></a><span class="w"> </span><span class="cm">/* 打印邻接表 */</span> <a id="__codelineno-14-63" name="__codelineno-14-63" href="#__codelineno-14-63"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-64" name="__codelineno-14-64" href="#__codelineno-14-64"></a><span class="w"> </span><span class="nx">print</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-14-64" name="__codelineno-14-64" href="#__codelineno-14-64"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-65" name="__codelineno-14-65" href="#__codelineno-14-65"></a><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;邻接表 =&quot;</span><span class="p">);</span> <a id="__codelineno-14-65" name="__codelineno-14-65" href="#__codelineno-14-65"></a>
<a id="__codelineno-14-66" name="__codelineno-14-66" href="#__codelineno-14-66"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">key</span><span class="p">,</span><span class="w"> </span><span class="nx">value</span><span class="p">]</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-14-66" name="__codelineno-14-66" href="#__codelineno-14-66"></a><span class="w"> </span><span class="cm">/* 打印邻接表 */</span>
<a id="__codelineno-14-67" name="__codelineno-14-67" href="#__codelineno-14-67"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span> <a id="__codelineno-14-67" name="__codelineno-14-67" href="#__codelineno-14-67"></a><span class="w"> </span><span class="nx">print</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-68" name="__codelineno-14-68" href="#__codelineno-14-68"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="nx">vertex</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="nx">value</span><span class="p">){</span> <a id="__codelineno-14-68" name="__codelineno-14-68" href="#__codelineno-14-68"></a><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;邻接表 =&quot;</span><span class="p">);</span>
<a id="__codelineno-14-69" name="__codelineno-14-69" href="#__codelineno-14-69"></a><span class="w"> </span><span class="nx">tmp</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">vertex</span><span class="p">.</span><span class="nx">val</span><span class="p">);</span> <a id="__codelineno-14-69" name="__codelineno-14-69" href="#__codelineno-14-69"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">key</span><span class="p">,</span><span class="w"> </span><span class="nx">value</span><span class="p">]</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-70" name="__codelineno-14-70" href="#__codelineno-14-70"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-70" name="__codelineno-14-70" href="#__codelineno-14-70"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
<a id="__codelineno-14-71" name="__codelineno-14-71" href="#__codelineno-14-71"></a><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">key</span><span class="p">.</span><span class="nx">val</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">tmp</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">&quot;,&quot;</span><span class="p">);</span> <a id="__codelineno-14-71" name="__codelineno-14-71" href="#__codelineno-14-71"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="nx">vertex</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="nx">value</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-72" name="__codelineno-14-72" href="#__codelineno-14-72"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-72" name="__codelineno-14-72" href="#__codelineno-14-72"></a><span class="w"> </span><span class="nx">tmp</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">vertex</span><span class="p">.</span><span class="nx">val</span><span class="p">);</span>
<a id="__codelineno-14-73" name="__codelineno-14-73" href="#__codelineno-14-73"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-14-73" name="__codelineno-14-73" href="#__codelineno-14-73"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-74" name="__codelineno-14-74" href="#__codelineno-14-74"></a><span class="p">}</span> <a id="__codelineno-14-74" name="__codelineno-14-74" href="#__codelineno-14-74"></a><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">key</span><span class="p">.</span><span class="nx">val</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">tmp</span><span class="p">.</span><span class="nx">join</span><span class="p">());</span>
<a id="__codelineno-14-75" name="__codelineno-14-75" href="#__codelineno-14-75"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-76" name="__codelineno-14-76" href="#__codelineno-14-76"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-14-77" name="__codelineno-14-77" href="#__codelineno-14-77"></a><span class="p">}</span>
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
@ -2629,7 +2632,7 @@
<a id="__codelineno-15-2" name="__codelineno-15-2" href="#__codelineno-15-2"></a><span class="kd">class</span><span class="w"> </span><span class="nx">GraphAdjList</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-15-2" name="__codelineno-15-2" href="#__codelineno-15-2"></a><span class="kd">class</span><span class="w"> </span><span class="nx">GraphAdjList</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-15-3" name="__codelineno-15-3" href="#__codelineno-15-3"></a><span class="w"> </span><span class="c1">// 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率</span> <a id="__codelineno-15-3" name="__codelineno-15-3" href="#__codelineno-15-3"></a><span class="w"> </span><span class="c1">// 邻接表,使用哈希表来代替链表,以提升删除边、删除顶点的效率</span>
<a id="__codelineno-15-4" name="__codelineno-15-4" href="#__codelineno-15-4"></a><span class="w"> </span><span class="c1">// 请注意adjList 中的元素是 Vertex 对象</span> <a id="__codelineno-15-4" name="__codelineno-15-4" href="#__codelineno-15-4"></a><span class="w"> </span><span class="c1">// 请注意adjList 中的元素是 Vertex 对象</span>
<a id="__codelineno-15-5" name="__codelineno-15-5" href="#__codelineno-15-5"></a><span class="w"> </span><span class="nx">adjList</span><span class="o">:</span><span class="w"> </span><span class="kt">Map</span><span class="o">&lt;</span><span class="nx">Vertex</span><span class="p">,</span><span class="w"> </span><span class="nb">Set</span><span class="o">&lt;</span><span class="nx">Vertex</span><span class="o">&gt;&gt;</span><span class="p">;</span> <a id="__codelineno-15-5" name="__codelineno-15-5" href="#__codelineno-15-5"></a><span class="w"> </span><span class="nx">adjList</span><span class="o">:</span><span class="w"> </span><span class="kt">Map</span><span class="o">&lt;</span><span class="nx">Vertex</span><span class="p">,</span><span class="w"> </span><span class="nx">Vertex</span><span class="p">[]</span><span class="o">&gt;</span><span class="p">;</span>
<a id="__codelineno-15-6" name="__codelineno-15-6" href="#__codelineno-15-6"></a> <a id="__codelineno-15-6" name="__codelineno-15-6" href="#__codelineno-15-6"></a>
<a id="__codelineno-15-7" name="__codelineno-15-7" href="#__codelineno-15-7"></a><span class="w"> </span><span class="cm">/* 构造方法 */</span> <a id="__codelineno-15-7" name="__codelineno-15-7" href="#__codelineno-15-7"></a><span class="w"> </span><span class="cm">/* 构造方法 */</span>
<a id="__codelineno-15-8" name="__codelineno-15-8" href="#__codelineno-15-8"></a><span class="w"> </span><span class="kr">constructor</span><span class="p">(</span><span class="nx">edges</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">[][])</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-15-8" name="__codelineno-15-8" href="#__codelineno-15-8"></a><span class="w"> </span><span class="kr">constructor</span><span class="p">(</span><span class="nx">edges</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">[][])</span><span class="w"> </span><span class="p">{</span>
@ -2653,8 +2656,8 @@
<a id="__codelineno-15-26" name="__codelineno-15-26" href="#__codelineno-15-26"></a><span class="w"> </span><span class="k">throw</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="ne">Error</span><span class="p">(</span><span class="s2">&quot;Illegal Argument Exception&quot;</span><span class="p">);</span> <a id="__codelineno-15-26" name="__codelineno-15-26" href="#__codelineno-15-26"></a><span class="w"> </span><span class="k">throw</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="ne">Error</span><span class="p">(</span><span class="s2">&quot;Illegal Argument Exception&quot;</span><span class="p">);</span>
<a id="__codelineno-15-27" name="__codelineno-15-27" href="#__codelineno-15-27"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-15-27" name="__codelineno-15-27" href="#__codelineno-15-27"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-15-28" name="__codelineno-15-28" href="#__codelineno-15-28"></a><span class="w"> </span><span class="c1">// 添加边 vet1 - vet2</span> <a id="__codelineno-15-28" name="__codelineno-15-28" href="#__codelineno-15-28"></a><span class="w"> </span><span class="c1">// 添加边 vet1 - vet2</span>
<a id="__codelineno-15-29" name="__codelineno-15-29" href="#__codelineno-15-29"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet1</span><span class="p">).</span><span class="nx">add</span><span class="p">(</span><span class="nx">vet2</span><span class="p">);</span> <a id="__codelineno-15-29" name="__codelineno-15-29" href="#__codelineno-15-29"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet1</span><span class="p">).</span><span class="nx">push</span><span class="p">(</span><span class="nx">vet2</span><span class="p">);</span>
<a id="__codelineno-15-30" name="__codelineno-15-30" href="#__codelineno-15-30"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet2</span><span class="p">).</span><span class="nx">add</span><span class="p">(</span><span class="nx">vet1</span><span class="p">);</span> <a id="__codelineno-15-30" name="__codelineno-15-30" href="#__codelineno-15-30"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet2</span><span class="p">).</span><span class="nx">push</span><span class="p">(</span><span class="nx">vet1</span><span class="p">);</span>
<a id="__codelineno-15-31" name="__codelineno-15-31" href="#__codelineno-15-31"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-15-31" name="__codelineno-15-31" href="#__codelineno-15-31"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-15-32" name="__codelineno-15-32" href="#__codelineno-15-32"></a> <a id="__codelineno-15-32" name="__codelineno-15-32" href="#__codelineno-15-32"></a>
<a id="__codelineno-15-33" name="__codelineno-15-33" href="#__codelineno-15-33"></a><span class="w"> </span><span class="cm">/* 删除边 */</span> <a id="__codelineno-15-33" name="__codelineno-15-33" href="#__codelineno-15-33"></a><span class="w"> </span><span class="cm">/* 删除边 */</span>
@ -2663,15 +2666,15 @@
<a id="__codelineno-15-36" name="__codelineno-15-36" href="#__codelineno-15-36"></a><span class="w"> </span><span class="k">throw</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="ne">Error</span><span class="p">(</span><span class="s2">&quot;Illegal Argument Exception&quot;</span><span class="p">);</span> <a id="__codelineno-15-36" name="__codelineno-15-36" href="#__codelineno-15-36"></a><span class="w"> </span><span class="k">throw</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="ne">Error</span><span class="p">(</span><span class="s2">&quot;Illegal Argument Exception&quot;</span><span class="p">);</span>
<a id="__codelineno-15-37" name="__codelineno-15-37" href="#__codelineno-15-37"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-15-37" name="__codelineno-15-37" href="#__codelineno-15-37"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-15-38" name="__codelineno-15-38" href="#__codelineno-15-38"></a><span class="w"> </span><span class="c1">// 删除边 vet1 - vet2</span> <a id="__codelineno-15-38" name="__codelineno-15-38" href="#__codelineno-15-38"></a><span class="w"> </span><span class="c1">// 删除边 vet1 - vet2</span>
<a id="__codelineno-15-39" name="__codelineno-15-39" href="#__codelineno-15-39"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet1</span><span class="p">).</span><span class="ow">delete</span><span class="p">(</span><span class="nx">vet2</span><span class="p">);</span> <a id="__codelineno-15-39" name="__codelineno-15-39" href="#__codelineno-15-39"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet1</span><span class="p">).</span><span class="nx">splice</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet1</span><span class="p">).</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">vet2</span><span class="p">),</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<a id="__codelineno-15-40" name="__codelineno-15-40" href="#__codelineno-15-40"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet2</span><span class="p">).</span><span class="ow">delete</span><span class="p">(</span><span class="nx">vet1</span><span class="p">);</span> <a id="__codelineno-15-40" name="__codelineno-15-40" href="#__codelineno-15-40"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet2</span><span class="p">).</span><span class="nx">splice</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">vet2</span><span class="p">).</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">vet1</span><span class="p">),</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<a id="__codelineno-15-41" name="__codelineno-15-41" href="#__codelineno-15-41"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-15-41" name="__codelineno-15-41" href="#__codelineno-15-41"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-15-42" name="__codelineno-15-42" href="#__codelineno-15-42"></a> <a id="__codelineno-15-42" name="__codelineno-15-42" href="#__codelineno-15-42"></a>
<a id="__codelineno-15-43" name="__codelineno-15-43" href="#__codelineno-15-43"></a><span class="w"> </span><span class="cm">/* 添加顶点 */</span> <a id="__codelineno-15-43" name="__codelineno-15-43" href="#__codelineno-15-43"></a><span class="w"> </span><span class="cm">/* 添加顶点 */</span>
<a id="__codelineno-15-44" name="__codelineno-15-44" href="#__codelineno-15-44"></a><span class="w"> </span><span class="nx">addVertex</span><span class="p">(</span><span class="nx">vet</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="ow">void</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-15-44" name="__codelineno-15-44" href="#__codelineno-15-44"></a><span class="w"> </span><span class="nx">addVertex</span><span class="p">(</span><span class="nx">vet</span><span class="o">:</span><span class="w"> </span><span class="kt">Vertex</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="ow">void</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-15-45" name="__codelineno-15-45" href="#__codelineno-15-45"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="nx">vet</span><span class="p">))</span><span class="w"> </span><span class="k">return</span><span class="p">;</span> <a id="__codelineno-15-45" name="__codelineno-15-45" href="#__codelineno-15-45"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="nx">vet</span><span class="p">))</span><span class="w"> </span><span class="k">return</span><span class="p">;</span>
<a id="__codelineno-15-46" name="__codelineno-15-46" href="#__codelineno-15-46"></a><span class="w"> </span><span class="c1">// 在邻接表中添加一个新链表</span> <a id="__codelineno-15-46" name="__codelineno-15-46" href="#__codelineno-15-46"></a><span class="w"> </span><span class="c1">// 在邻接表中添加一个新链表</span>
<a id="__codelineno-15-47" name="__codelineno-15-47" href="#__codelineno-15-47"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">vet</span><span class="p">,</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Set</span><span class="p">());</span> <a id="__codelineno-15-47" name="__codelineno-15-47" href="#__codelineno-15-47"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">vet</span><span class="p">,</span><span class="w"> </span><span class="p">[]);</span>
<a id="__codelineno-15-48" name="__codelineno-15-48" href="#__codelineno-15-48"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-15-48" name="__codelineno-15-48" href="#__codelineno-15-48"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-15-49" name="__codelineno-15-49" href="#__codelineno-15-49"></a> <a id="__codelineno-15-49" name="__codelineno-15-49" href="#__codelineno-15-49"></a>
<a id="__codelineno-15-50" name="__codelineno-15-50" href="#__codelineno-15-50"></a><span class="w"> </span><span class="cm">/* 删除顶点 */</span> <a id="__codelineno-15-50" name="__codelineno-15-50" href="#__codelineno-15-50"></a><span class="w"> </span><span class="cm">/* 删除顶点 */</span>
@ -2683,22 +2686,25 @@
<a id="__codelineno-15-56" name="__codelineno-15-56" href="#__codelineno-15-56"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="ow">delete</span><span class="p">(</span><span class="nx">vet</span><span class="p">);</span> <a id="__codelineno-15-56" name="__codelineno-15-56" href="#__codelineno-15-56"></a><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="ow">delete</span><span class="p">(</span><span class="nx">vet</span><span class="p">);</span>
<a id="__codelineno-15-57" name="__codelineno-15-57" href="#__codelineno-15-57"></a><span class="w"> </span><span class="c1">// 遍历其它顶点的链表,删除所有包含 vet 的边</span> <a id="__codelineno-15-57" name="__codelineno-15-57" href="#__codelineno-15-57"></a><span class="w"> </span><span class="c1">// 遍历其它顶点的链表,删除所有包含 vet 的边</span>
<a id="__codelineno-15-58" name="__codelineno-15-58" href="#__codelineno-15-58"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">set</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">values</span><span class="p">())</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-15-58" name="__codelineno-15-58" href="#__codelineno-15-58"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">set</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">values</span><span class="p">())</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-15-59" name="__codelineno-15-59" href="#__codelineno-15-59"></a><span class="w"> </span><span class="nx">set</span><span class="p">.</span><span class="ow">delete</span><span class="p">(</span><span class="nx">vet</span><span class="p">);</span> <a id="__codelineno-15-59" name="__codelineno-15-59" href="#__codelineno-15-59"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">index</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">set</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">vet</span><span class="p">);</span>
<a id="__codelineno-15-60" name="__codelineno-15-60" href="#__codelineno-15-60"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-15-60" name="__codelineno-15-60" href="#__codelineno-15-60"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">index</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="o">-</span><span class="mf">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-15-61" name="__codelineno-15-61" href="#__codelineno-15-61"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-15-61" name="__codelineno-15-61" href="#__codelineno-15-61"></a><span class="w"> </span><span class="nx">set</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<a id="__codelineno-15-62" name="__codelineno-15-62" href="#__codelineno-15-62"></a> <a id="__codelineno-15-62" name="__codelineno-15-62" href="#__codelineno-15-62"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-15-63" name="__codelineno-15-63" href="#__codelineno-15-63"></a><span class="w"> </span><span class="cm">/* 打印邻接表 */</span> <a id="__codelineno-15-63" name="__codelineno-15-63" href="#__codelineno-15-63"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-15-64" name="__codelineno-15-64" href="#__codelineno-15-64"></a><span class="w"> </span><span class="nx">print</span><span class="p">()</span><span class="o">:</span><span class="w"> </span><span class="ow">void</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-15-64" name="__codelineno-15-64" href="#__codelineno-15-64"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-15-65" name="__codelineno-15-65" href="#__codelineno-15-65"></a><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;邻接表 =&quot;</span><span class="p">);</span> <a id="__codelineno-15-65" name="__codelineno-15-65" href="#__codelineno-15-65"></a>
<a id="__codelineno-15-66" name="__codelineno-15-66" href="#__codelineno-15-66"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">key</span><span class="p">,</span><span class="w"> </span><span class="nx">value</span><span class="p">]</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">entries</span><span class="p">())</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-15-66" name="__codelineno-15-66" href="#__codelineno-15-66"></a><span class="w"> </span><span class="cm">/* 打印邻接表 */</span>
<a id="__codelineno-15-67" name="__codelineno-15-67" href="#__codelineno-15-67"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span> <a id="__codelineno-15-67" name="__codelineno-15-67" href="#__codelineno-15-67"></a><span class="w"> </span><span class="nx">print</span><span class="p">()</span><span class="o">:</span><span class="w"> </span><span class="ow">void</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-15-68" name="__codelineno-15-68" href="#__codelineno-15-68"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="nx">vertex</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="nx">value</span><span class="p">){</span> <a id="__codelineno-15-68" name="__codelineno-15-68" href="#__codelineno-15-68"></a><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;邻接表 =&quot;</span><span class="p">);</span>
<a id="__codelineno-15-69" name="__codelineno-15-69" href="#__codelineno-15-69"></a><span class="w"> </span><span class="nx">tmp</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">vertex</span><span class="p">.</span><span class="nx">val</span><span class="p">);</span> <a id="__codelineno-15-69" name="__codelineno-15-69" href="#__codelineno-15-69"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">key</span><span class="p">,</span><span class="w"> </span><span class="nx">value</span><span class="p">]</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">adjList</span><span class="p">.</span><span class="nx">entries</span><span class="p">())</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-15-70" name="__codelineno-15-70" href="#__codelineno-15-70"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-15-70" name="__codelineno-15-70" href="#__codelineno-15-70"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[];</span>
<a id="__codelineno-15-71" name="__codelineno-15-71" href="#__codelineno-15-71"></a><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">key</span><span class="p">.</span><span class="nx">val</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">tmp</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">&quot;,&quot;</span><span class="p">);</span> <a id="__codelineno-15-71" name="__codelineno-15-71" href="#__codelineno-15-71"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">const</span><span class="w"> </span><span class="nx">vertex</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="nx">value</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-15-72" name="__codelineno-15-72" href="#__codelineno-15-72"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-15-72" name="__codelineno-15-72" href="#__codelineno-15-72"></a><span class="w"> </span><span class="nx">tmp</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">vertex</span><span class="p">.</span><span class="nx">val</span><span class="p">);</span>
<a id="__codelineno-15-73" name="__codelineno-15-73" href="#__codelineno-15-73"></a><span class="w"> </span><span class="p">}</span> <a id="__codelineno-15-73" name="__codelineno-15-73" href="#__codelineno-15-73"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-15-74" name="__codelineno-15-74" href="#__codelineno-15-74"></a><span class="p">}</span> <a id="__codelineno-15-74" name="__codelineno-15-74" href="#__codelineno-15-74"></a><span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">key</span><span class="p">.</span><span class="nx">val</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">tmp</span><span class="p">.</span><span class="nx">join</span><span class="p">());</span>
<a id="__codelineno-15-75" name="__codelineno-15-75" href="#__codelineno-15-75"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-15-76" name="__codelineno-15-76" href="#__codelineno-15-76"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-15-77" name="__codelineno-15-77" href="#__codelineno-15-77"></a><span class="p">}</span>
</code></pre></div> </code></pre></div>
</div> </div>
<div class="tabbed-block"> <div class="tabbed-block">
@ -2794,7 +2800,7 @@
<a id="__codelineno-18-2" name="__codelineno-18-2" href="#__codelineno-18-2"></a><span class="kd">class</span> <span class="nc">GraphAdjList</span> <span class="p">{</span> <a id="__codelineno-18-2" name="__codelineno-18-2" href="#__codelineno-18-2"></a><span class="kd">class</span> <span class="nc">GraphAdjList</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-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="c1">// 请注意adjList 中的元素是 Vertex 对象</span> <a id="__codelineno-18-4" name="__codelineno-18-4" href="#__codelineno-18-4"></a> <span class="c1">// 请注意adjList 中的元素是 Vertex 对象</span>
<a id="__codelineno-18-5" name="__codelineno-18-5" href="#__codelineno-18-5"></a> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">adjList</span><span class="p">:</span> <span class="p">[</span><span class="n">Vertex</span><span class="p">:</span> <span class="n">Set</span><span class="p">&lt;</span><span class="n">Vertex</span><span class="p">&gt;]</span> <a id="__codelineno-18-5" name="__codelineno-18-5" href="#__codelineno-18-5"></a> <span class="kd">private</span> <span class="kd">var</span> <span class="nv">adjList</span><span class="p">:</span> <span class="p">[</span><span class="n">Vertex</span><span class="p">:</span> <span class="p">[</span><span class="n">Vertex</span><span class="p">]]</span>
<a id="__codelineno-18-6" name="__codelineno-18-6" href="#__codelineno-18-6"></a> <a id="__codelineno-18-6" name="__codelineno-18-6" href="#__codelineno-18-6"></a>
<a id="__codelineno-18-7" name="__codelineno-18-7" href="#__codelineno-18-7"></a> <span class="cm">/* 构造方法 */</span> <a id="__codelineno-18-7" name="__codelineno-18-7" href="#__codelineno-18-7"></a> <span class="cm">/* 构造方法 */</span>
<a id="__codelineno-18-8" name="__codelineno-18-8" href="#__codelineno-18-8"></a> <span class="kd">init</span><span class="p">(</span><span class="n">edges</span><span class="p">:</span> <span class="p">[[</span><span class="n">Vertex</span><span class="p">]])</span> <span class="p">{</span> <a id="__codelineno-18-8" name="__codelineno-18-8" href="#__codelineno-18-8"></a> <span class="kd">init</span><span class="p">(</span><span class="n">edges</span><span class="p">:</span> <span class="p">[[</span><span class="n">Vertex</span><span class="p">]])</span> <span class="p">{</span>
@ -2818,8 +2824,8 @@
<a id="__codelineno-18-26" name="__codelineno-18-26" href="#__codelineno-18-26"></a> <span class="bp">fatalError</span><span class="p">(</span><span class="s">&quot;参数错误&quot;</span><span class="p">)</span> <a id="__codelineno-18-26" name="__codelineno-18-26" href="#__codelineno-18-26"></a> <span class="bp">fatalError</span><span class="p">(</span><span class="s">&quot;参数错误&quot;</span><span class="p">)</span>
<a id="__codelineno-18-27" name="__codelineno-18-27" href="#__codelineno-18-27"></a> <span class="p">}</span> <a id="__codelineno-18-27" name="__codelineno-18-27" href="#__codelineno-18-27"></a> <span class="p">}</span>
<a id="__codelineno-18-28" name="__codelineno-18-28" href="#__codelineno-18-28"></a> <span class="c1">// 添加边 vet1 - vet2</span> <a id="__codelineno-18-28" name="__codelineno-18-28" href="#__codelineno-18-28"></a> <span class="c1">// 添加边 vet1 - vet2</span>
<a id="__codelineno-18-29" name="__codelineno-18-29" href="#__codelineno-18-29"></a> <span class="n">adjList</span><span class="p">[</span><span class="n">vet1</span><span class="p">]?.</span><span class="bp">insert</span><span class="p">(</span><span class="n">vet2</span><span class="p">)</span> <a id="__codelineno-18-29" name="__codelineno-18-29" href="#__codelineno-18-29"></a> <span class="n">adjList</span><span class="p">[</span><span class="n">vet1</span><span class="p">]?.</span><span class="n">append</span><span class="p">(</span><span class="n">vet2</span><span class="p">)</span>
<a id="__codelineno-18-30" name="__codelineno-18-30" href="#__codelineno-18-30"></a> <span class="n">adjList</span><span class="p">[</span><span class="n">vet2</span><span class="p">]?.</span><span class="bp">insert</span><span class="p">(</span><span class="n">vet1</span><span class="p">)</span> <a id="__codelineno-18-30" name="__codelineno-18-30" href="#__codelineno-18-30"></a> <span class="n">adjList</span><span class="p">[</span><span class="n">vet2</span><span class="p">]?.</span><span class="n">append</span><span class="p">(</span><span class="n">vet1</span><span class="p">)</span>
<a id="__codelineno-18-31" name="__codelineno-18-31" href="#__codelineno-18-31"></a> <span class="p">}</span> <a id="__codelineno-18-31" name="__codelineno-18-31" href="#__codelineno-18-31"></a> <span class="p">}</span>
<a id="__codelineno-18-32" name="__codelineno-18-32" href="#__codelineno-18-32"></a> <a id="__codelineno-18-32" name="__codelineno-18-32" href="#__codelineno-18-32"></a>
<a id="__codelineno-18-33" name="__codelineno-18-33" href="#__codelineno-18-33"></a> <span class="cm">/* 删除边 */</span> <a id="__codelineno-18-33" name="__codelineno-18-33" href="#__codelineno-18-33"></a> <span class="cm">/* 删除边 */</span>
@ -2828,8 +2834,8 @@
<a id="__codelineno-18-36" name="__codelineno-18-36" href="#__codelineno-18-36"></a> <span class="bp">fatalError</span><span class="p">(</span><span class="s">&quot;参数错误&quot;</span><span class="p">)</span> <a id="__codelineno-18-36" name="__codelineno-18-36" href="#__codelineno-18-36"></a> <span class="bp">fatalError</span><span class="p">(</span><span class="s">&quot;参数错误&quot;</span><span class="p">)</span>
<a id="__codelineno-18-37" name="__codelineno-18-37" href="#__codelineno-18-37"></a> <span class="p">}</span> <a id="__codelineno-18-37" name="__codelineno-18-37" href="#__codelineno-18-37"></a> <span class="p">}</span>
<a id="__codelineno-18-38" name="__codelineno-18-38" href="#__codelineno-18-38"></a> <span class="c1">// 删除边 vet1 - vet2</span> <a id="__codelineno-18-38" name="__codelineno-18-38" href="#__codelineno-18-38"></a> <span class="c1">// 删除边 vet1 - vet2</span>
<a id="__codelineno-18-39" name="__codelineno-18-39" href="#__codelineno-18-39"></a> <span class="n">adjList</span><span class="p">[</span><span class="n">vet1</span><span class="p">]?.</span><span class="n">remove</span><span class="p">(</span><span class="n">vet2</span><span class="p">)</span> <a id="__codelineno-18-39" name="__codelineno-18-39" href="#__codelineno-18-39"></a> <span class="n">adjList</span><span class="p">[</span><span class="n">vet1</span><span class="p">]?.</span><span class="bp">removeAll</span><span class="p">(</span><span class="k">where</span><span class="p">:</span> <span class="p">{</span> <span class="nv">$0</span> <span class="p">==</span> <span class="n">vet2</span> <span class="p">})</span>
<a id="__codelineno-18-40" name="__codelineno-18-40" href="#__codelineno-18-40"></a> <span class="n">adjList</span><span class="p">[</span><span class="n">vet2</span><span class="p">]?.</span><span class="n">remove</span><span class="p">(</span><span class="n">vet1</span><span class="p">)</span> <a id="__codelineno-18-40" name="__codelineno-18-40" href="#__codelineno-18-40"></a> <span class="n">adjList</span><span class="p">[</span><span class="n">vet2</span><span class="p">]?.</span><span class="bp">removeAll</span><span class="p">(</span><span class="k">where</span><span class="p">:</span> <span class="p">{</span> <span class="nv">$0</span> <span class="p">==</span> <span class="n">vet1</span> <span class="p">})</span>
<a id="__codelineno-18-41" name="__codelineno-18-41" href="#__codelineno-18-41"></a> <span class="p">}</span> <a id="__codelineno-18-41" name="__codelineno-18-41" href="#__codelineno-18-41"></a> <span class="p">}</span>
<a id="__codelineno-18-42" name="__codelineno-18-42" href="#__codelineno-18-42"></a> <a id="__codelineno-18-42" name="__codelineno-18-42" href="#__codelineno-18-42"></a>
<a id="__codelineno-18-43" name="__codelineno-18-43" href="#__codelineno-18-43"></a> <span class="cm">/* 添加顶点 */</span> <a id="__codelineno-18-43" name="__codelineno-18-43" href="#__codelineno-18-43"></a> <span class="cm">/* 添加顶点 */</span>
@ -2850,7 +2856,7 @@
<a id="__codelineno-18-58" name="__codelineno-18-58" href="#__codelineno-18-58"></a> <span class="n">adjList</span><span class="p">.</span><span class="n">removeValue</span><span class="p">(</span><span class="n">forKey</span><span class="p">:</span> <span class="n">vet</span><span class="p">)</span> <a id="__codelineno-18-58" name="__codelineno-18-58" href="#__codelineno-18-58"></a> <span class="n">adjList</span><span class="p">.</span><span class="n">removeValue</span><span class="p">(</span><span class="n">forKey</span><span class="p">:</span> <span class="n">vet</span><span class="p">)</span>
<a id="__codelineno-18-59" name="__codelineno-18-59" href="#__codelineno-18-59"></a> <span class="c1">// 遍历其它顶点的链表,删除所有包含 vet 的边</span> <a id="__codelineno-18-59" name="__codelineno-18-59" href="#__codelineno-18-59"></a> <span class="c1">// 遍历其它顶点的链表,删除所有包含 vet 的边</span>
<a id="__codelineno-18-60" name="__codelineno-18-60" href="#__codelineno-18-60"></a> <span class="k">for</span> <span class="n">key</span> <span class="k">in</span> <span class="n">adjList</span><span class="p">.</span><span class="n">keys</span> <span class="p">{</span> <a id="__codelineno-18-60" name="__codelineno-18-60" href="#__codelineno-18-60"></a> <span class="k">for</span> <span class="n">key</span> <span class="k">in</span> <span class="n">adjList</span><span class="p">.</span><span class="n">keys</span> <span class="p">{</span>
<a id="__codelineno-18-61" name="__codelineno-18-61" href="#__codelineno-18-61"></a> <span class="n">adjList</span><span class="p">[</span><span class="n">key</span><span class="p">]?.</span><span class="n">remove</span><span class="p">(</span><span class="n">vet</span><span class="p">)</span> <a id="__codelineno-18-61" name="__codelineno-18-61" href="#__codelineno-18-61"></a> <span class="n">adjList</span><span class="p">[</span><span class="n">key</span><span class="p">]?.</span><span class="bp">removeAll</span><span class="p">(</span><span class="k">where</span><span class="p">:</span> <span class="p">{</span> <span class="nv">$0</span> <span class="p">==</span> <span class="n">vet</span> <span class="p">})</span>
<a id="__codelineno-18-62" name="__codelineno-18-62" href="#__codelineno-18-62"></a> <span class="p">}</span> <a id="__codelineno-18-62" name="__codelineno-18-62" href="#__codelineno-18-62"></a> <span class="p">}</span>
<a id="__codelineno-18-63" name="__codelineno-18-63" href="#__codelineno-18-63"></a> <span class="p">}</span> <a id="__codelineno-18-63" name="__codelineno-18-63" href="#__codelineno-18-63"></a> <span class="p">}</span>
<a id="__codelineno-18-64" name="__codelineno-18-64" href="#__codelineno-18-64"></a> <a id="__codelineno-18-64" name="__codelineno-18-64" href="#__codelineno-18-64"></a>

View file

@ -2052,7 +2052,7 @@
<div class="tabbed-block"> <div class="tabbed-block">
<div class="highlight"><span class="filename">quick_sort.java</span><pre><span></span><code><a id="__codelineno-20-1" name="__codelineno-20-1" href="#__codelineno-20-1"></a><span class="cm">/* 选取三个元素的中位数 */</span> <div class="highlight"><span class="filename">quick_sort.java</span><pre><span></span><code><a id="__codelineno-20-1" name="__codelineno-20-1" href="#__codelineno-20-1"></a><span class="cm">/* 选取三个元素的中位数 */</span>
<a id="__codelineno-20-2" name="__codelineno-20-2" href="#__codelineno-20-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">medianThree</span><span class="p">(</span><span class="kt">int</span><span class="o">[]</span><span class="w"> </span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">left</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">mid</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">right</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-20-2" name="__codelineno-20-2" href="#__codelineno-20-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">medianThree</span><span class="p">(</span><span class="kt">int</span><span class="o">[]</span><span class="w"> </span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">left</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">mid</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">right</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-20-3" name="__codelineno-20-3" href="#__codelineno-20-3"></a><span class="w"> </span><span class="c1">// 使用了异或操作来简化代码</span> <a id="__codelineno-20-3" name="__codelineno-20-3" href="#__codelineno-20-3"></a><span class="w"> </span><span class="c1">// 此处使用异或运算来简化代码</span>
<a id="__codelineno-20-4" name="__codelineno-20-4" href="#__codelineno-20-4"></a><span class="w"> </span><span class="c1">// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span> <a id="__codelineno-20-4" name="__codelineno-20-4" href="#__codelineno-20-4"></a><span class="w"> </span><span class="c1">// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span>
<a id="__codelineno-20-5" name="__codelineno-20-5" href="#__codelineno-20-5"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">((</span><span class="n">nums</span><span class="o">[</span><span class="n">left</span><span class="o">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">mid</span><span class="o">]</span><span class="p">)</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="p">(</span><span class="n">nums</span><span class="o">[</span><span class="n">left</span><span class="o">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">right</span><span class="o">]</span><span class="p">))</span> <a id="__codelineno-20-5" name="__codelineno-20-5" href="#__codelineno-20-5"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">((</span><span class="n">nums</span><span class="o">[</span><span class="n">left</span><span class="o">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">mid</span><span class="o">]</span><span class="p">)</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="p">(</span><span class="n">nums</span><span class="o">[</span><span class="n">left</span><span class="o">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">right</span><span class="o">]</span><span class="p">))</span>
<a id="__codelineno-20-6" name="__codelineno-20-6" href="#__codelineno-20-6"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">left</span><span class="p">;</span> <a id="__codelineno-20-6" name="__codelineno-20-6" href="#__codelineno-20-6"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">left</span><span class="p">;</span>
@ -2085,7 +2085,7 @@
<div class="tabbed-block"> <div class="tabbed-block">
<div class="highlight"><span class="filename">quick_sort.cpp</span><pre><span></span><code><a id="__codelineno-21-1" name="__codelineno-21-1" href="#__codelineno-21-1"></a><span class="cm">/* 选取三个元素的中位数 */</span> <div class="highlight"><span class="filename">quick_sort.cpp</span><pre><span></span><code><a id="__codelineno-21-1" name="__codelineno-21-1" href="#__codelineno-21-1"></a><span class="cm">/* 选取三个元素的中位数 */</span>
<a id="__codelineno-21-2" name="__codelineno-21-2" href="#__codelineno-21-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">medianThree</span><span class="p">(</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;&amp;</span><span class="w"> </span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">left</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">mid</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">right</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-21-2" name="__codelineno-21-2" href="#__codelineno-21-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">medianThree</span><span class="p">(</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;&amp;</span><span class="w"> </span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">left</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">mid</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">right</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-21-3" name="__codelineno-21-3" href="#__codelineno-21-3"></a><span class="w"> </span><span class="c1">// 使用了异或操作来简化代码</span> <a id="__codelineno-21-3" name="__codelineno-21-3" href="#__codelineno-21-3"></a><span class="w"> </span><span class="c1">// 此处使用异或运算来简化代码</span>
<a id="__codelineno-21-4" name="__codelineno-21-4" href="#__codelineno-21-4"></a><span class="w"> </span><span class="c1">// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span> <a id="__codelineno-21-4" name="__codelineno-21-4" href="#__codelineno-21-4"></a><span class="w"> </span><span class="c1">// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span>
<a id="__codelineno-21-5" name="__codelineno-21-5" href="#__codelineno-21-5"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">((</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">mid</span><span class="p">])</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="p">(</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">right</span><span class="p">]))</span> <a id="__codelineno-21-5" name="__codelineno-21-5" href="#__codelineno-21-5"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">((</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">mid</span><span class="p">])</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="p">(</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">right</span><span class="p">]))</span>
<a id="__codelineno-21-6" name="__codelineno-21-6" href="#__codelineno-21-6"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">left</span><span class="p">;</span> <a id="__codelineno-21-6" name="__codelineno-21-6" href="#__codelineno-21-6"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">left</span><span class="p">;</span>
@ -2118,11 +2118,11 @@
<div class="tabbed-block"> <div class="tabbed-block">
<div class="highlight"><span class="filename">quick_sort.py</span><pre><span></span><code><a id="__codelineno-22-1" name="__codelineno-22-1" href="#__codelineno-22-1"></a><span class="sd">&quot;&quot;&quot; 选取三个元素的中位数 &quot;&quot;&quot;</span> <div class="highlight"><span class="filename">quick_sort.py</span><pre><span></span><code><a id="__codelineno-22-1" name="__codelineno-22-1" href="#__codelineno-22-1"></a><span class="sd">&quot;&quot;&quot; 选取三个元素的中位数 &quot;&quot;&quot;</span>
<a id="__codelineno-22-2" name="__codelineno-22-2" href="#__codelineno-22-2"></a><span class="k">def</span> <span class="nf">median_three</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nums</span><span class="p">,</span> <span class="n">left</span><span class="p">,</span> <span class="n">mid</span><span class="p">,</span> <span class="n">right</span><span class="p">):</span> <a id="__codelineno-22-2" name="__codelineno-22-2" href="#__codelineno-22-2"></a><span class="k">def</span> <span class="nf">median_three</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nums</span><span class="p">,</span> <span class="n">left</span><span class="p">,</span> <span class="n">mid</span><span class="p">,</span> <span class="n">right</span><span class="p">):</span>
<a id="__codelineno-22-3" name="__codelineno-22-3" href="#__codelineno-22-3"></a> <span class="c1"># 使用了异或操作来简化代码</span> <a id="__codelineno-22-3" name="__codelineno-22-3" href="#__codelineno-22-3"></a> <span class="c1"># 此处使用异或运算来简化代码</span>
<a id="__codelineno-22-4" name="__codelineno-22-4" href="#__codelineno-22-4"></a> <span class="c1"># 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span> <a id="__codelineno-22-4" name="__codelineno-22-4" href="#__codelineno-22-4"></a> <span class="c1"># 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span>
<a id="__codelineno-22-5" name="__codelineno-22-5" href="#__codelineno-22-5"></a> <span class="k">if</span> <span class="p">(</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">nums</span><span class="p">[</span><span class="n">mid</span><span class="p">])</span> <span class="o">^</span> <span class="p">(</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">nums</span><span class="p">[</span><span class="n">right</span><span class="p">]):</span> <a id="__codelineno-22-5" name="__codelineno-22-5" href="#__codelineno-22-5"></a> <span class="k">if</span> <span class="p">(</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">nums</span><span class="p">[</span><span class="n">mid</span><span class="p">])</span> <span class="o">^</span> <span class="p">(</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">nums</span><span class="p">[</span><span class="n">right</span><span class="p">]):</span>
<a id="__codelineno-22-6" name="__codelineno-22-6" href="#__codelineno-22-6"></a> <span class="k">return</span> <span class="n">left</span> <a id="__codelineno-22-6" name="__codelineno-22-6" href="#__codelineno-22-6"></a> <span class="k">return</span> <span class="n">left</span>
<a id="__codelineno-22-7" name="__codelineno-22-7" href="#__codelineno-22-7"></a> <span class="k">elif</span> <span class="p">(</span><span class="n">nums</span><span class="p">[</span><span class="n">mid</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">])</span> <span class="o">^</span> <span class="p">(</span><span class="n">nums</span><span class="p">[</span><span class="n">mid</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">nums</span><span class="p">[</span><span class="n">right</span><span class="p">]):</span> <a id="__codelineno-22-7" name="__codelineno-22-7" href="#__codelineno-22-7"></a> <span class="k">elif</span> <span class="p">(</span><span class="n">nums</span><span class="p">[</span><span class="n">mid</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">])</span> <span class="o">^</span> <span class="p">(</span><span class="n">nums</span><span class="p">[</span><span class="n">mid</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">nums</span><span class="p">[</span><span class="n">right</span><span class="p">]):</span>
<a id="__codelineno-22-8" name="__codelineno-22-8" href="#__codelineno-22-8"></a> <span class="k">return</span> <span class="n">mid</span> <a id="__codelineno-22-8" name="__codelineno-22-8" href="#__codelineno-22-8"></a> <span class="k">return</span> <span class="n">mid</span>
<a id="__codelineno-22-9" name="__codelineno-22-9" href="#__codelineno-22-9"></a> <span class="k">return</span> <span class="n">right</span> <a id="__codelineno-22-9" name="__codelineno-22-9" href="#__codelineno-22-9"></a> <span class="k">return</span> <span class="n">right</span>
<a id="__codelineno-22-10" name="__codelineno-22-10" href="#__codelineno-22-10"></a> <a id="__codelineno-22-10" name="__codelineno-22-10" href="#__codelineno-22-10"></a>
@ -2149,7 +2149,7 @@
<div class="tabbed-block"> <div class="tabbed-block">
<div class="highlight"><span class="filename">quick_sort.go</span><pre><span></span><code><a id="__codelineno-23-1" name="__codelineno-23-1" href="#__codelineno-23-1"></a><span class="cm">/* 选取三个元素的中位数 */</span> <div class="highlight"><span class="filename">quick_sort.go</span><pre><span></span><code><a id="__codelineno-23-1" name="__codelineno-23-1" href="#__codelineno-23-1"></a><span class="cm">/* 选取三个元素的中位数 */</span>
<a id="__codelineno-23-2" name="__codelineno-23-2" href="#__codelineno-23-2"></a><span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">q</span><span class="w"> </span><span class="o">*</span><span class="nx">quickSortMedian</span><span class="p">)</span><span class="w"> </span><span class="nx">medianThree</span><span class="p">(</span><span class="nx">nums</span><span class="w"> </span><span class="p">[]</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="nx">left</span><span class="p">,</span><span class="w"> </span><span class="nx">mid</span><span class="p">,</span><span class="w"> </span><span class="nx">right</span><span class="w"> </span><span class="kt">int</span><span class="p">)</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-23-2" name="__codelineno-23-2" href="#__codelineno-23-2"></a><span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">q</span><span class="w"> </span><span class="o">*</span><span class="nx">quickSortMedian</span><span class="p">)</span><span class="w"> </span><span class="nx">medianThree</span><span class="p">(</span><span class="nx">nums</span><span class="w"> </span><span class="p">[]</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="nx">left</span><span class="p">,</span><span class="w"> </span><span class="nx">mid</span><span class="p">,</span><span class="w"> </span><span class="nx">right</span><span class="w"> </span><span class="kt">int</span><span class="p">)</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-23-3" name="__codelineno-23-3" href="#__codelineno-23-3"></a><span class="w"> </span><span class="c1">// 使用了异或操作来简化代码(!= 在这里起到异或的作用)</span> <a id="__codelineno-23-3" name="__codelineno-23-3" href="#__codelineno-23-3"></a><span class="w"> </span><span class="c1">// 此处使用异或运算来简化代码(!= 在这里起到异或的作用)</span>
<a id="__codelineno-23-4" name="__codelineno-23-4" href="#__codelineno-23-4"></a><span class="w"> </span><span class="c1">// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span> <a id="__codelineno-23-4" name="__codelineno-23-4" href="#__codelineno-23-4"></a><span class="w"> </span><span class="c1">// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span>
<a id="__codelineno-23-5" name="__codelineno-23-5" href="#__codelineno-23-5"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">nums</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">mid</span><span class="p">])</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="p">(</span><span class="nx">nums</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">right</span><span class="p">])</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-23-5" name="__codelineno-23-5" href="#__codelineno-23-5"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">nums</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">mid</span><span class="p">])</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="p">(</span><span class="nx">nums</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">right</span><span class="p">])</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-23-6" name="__codelineno-23-6" href="#__codelineno-23-6"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">left</span> <a id="__codelineno-23-6" name="__codelineno-23-6" href="#__codelineno-23-6"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">left</span>
@ -2186,7 +2186,7 @@
<div class="tabbed-block"> <div class="tabbed-block">
<div class="highlight"><span class="filename">quick_sort.js</span><pre><span></span><code><a id="__codelineno-24-1" name="__codelineno-24-1" href="#__codelineno-24-1"></a><span class="cm">/* 选取三个元素的中位数 */</span> <div class="highlight"><span class="filename">quick_sort.js</span><pre><span></span><code><a id="__codelineno-24-1" name="__codelineno-24-1" href="#__codelineno-24-1"></a><span class="cm">/* 选取三个元素的中位数 */</span>
<a id="__codelineno-24-2" name="__codelineno-24-2" href="#__codelineno-24-2"></a><span class="nx">medianThree</span><span class="p">(</span><span class="nx">nums</span><span class="p">,</span><span class="w"> </span><span class="nx">left</span><span class="p">,</span><span class="w"> </span><span class="nx">mid</span><span class="p">,</span><span class="w"> </span><span class="nx">right</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-24-2" name="__codelineno-24-2" href="#__codelineno-24-2"></a><span class="nx">medianThree</span><span class="p">(</span><span class="nx">nums</span><span class="p">,</span><span class="w"> </span><span class="nx">left</span><span class="p">,</span><span class="w"> </span><span class="nx">mid</span><span class="p">,</span><span class="w"> </span><span class="nx">right</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-24-3" name="__codelineno-24-3" href="#__codelineno-24-3"></a><span class="w"> </span><span class="c1">// 使用了异或操作来简化代码</span> <a id="__codelineno-24-3" name="__codelineno-24-3" href="#__codelineno-24-3"></a><span class="w"> </span><span class="c1">// 此处使用异或运算来简化代码</span>
<a id="__codelineno-24-4" name="__codelineno-24-4" href="#__codelineno-24-4"></a><span class="w"> </span><span class="c1">// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span> <a id="__codelineno-24-4" name="__codelineno-24-4" href="#__codelineno-24-4"></a><span class="w"> </span><span class="c1">// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span>
<a id="__codelineno-24-5" name="__codelineno-24-5" href="#__codelineno-24-5"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">((</span><span class="nx">nums</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">mid</span><span class="p">])</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="p">(</span><span class="nx">nums</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">right</span><span class="p">]))</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">left</span><span class="p">;</span> <a id="__codelineno-24-5" name="__codelineno-24-5" href="#__codelineno-24-5"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">((</span><span class="nx">nums</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">mid</span><span class="p">])</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="p">(</span><span class="nx">nums</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">right</span><span class="p">]))</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">left</span><span class="p">;</span>
<a id="__codelineno-24-6" name="__codelineno-24-6" href="#__codelineno-24-6"></a><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">((</span><span class="nx">nums</span><span class="p">[</span><span class="nx">mid</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">left</span><span class="p">])</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="p">(</span><span class="nx">nums</span><span class="p">[</span><span class="nx">mid</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">right</span><span class="p">]))</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">mid</span><span class="p">;</span> <a id="__codelineno-24-6" name="__codelineno-24-6" href="#__codelineno-24-6"></a><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">((</span><span class="nx">nums</span><span class="p">[</span><span class="nx">mid</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">left</span><span class="p">])</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="p">(</span><span class="nx">nums</span><span class="p">[</span><span class="nx">mid</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">right</span><span class="p">]))</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">mid</span><span class="p">;</span>
@ -2214,7 +2214,7 @@
<div class="tabbed-block"> <div class="tabbed-block">
<div class="highlight"><span class="filename">quick_sort.ts</span><pre><span></span><code><a id="__codelineno-25-1" name="__codelineno-25-1" href="#__codelineno-25-1"></a><span class="cm">/* 选取三个元素的中位数 */</span> <div class="highlight"><span class="filename">quick_sort.ts</span><pre><span></span><code><a id="__codelineno-25-1" name="__codelineno-25-1" href="#__codelineno-25-1"></a><span class="cm">/* 选取三个元素的中位数 */</span>
<a id="__codelineno-25-2" name="__codelineno-25-2" href="#__codelineno-25-2"></a><span class="nx">medianThree</span><span class="p">(</span><span class="nx">nums</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">[],</span><span class="w"> </span><span class="nx">left</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">,</span><span class="w"> </span><span class="nx">mid</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">,</span><span class="w"> </span><span class="nx">right</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-25-2" name="__codelineno-25-2" href="#__codelineno-25-2"></a><span class="nx">medianThree</span><span class="p">(</span><span class="nx">nums</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">[],</span><span class="w"> </span><span class="nx">left</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">,</span><span class="w"> </span><span class="nx">mid</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">,</span><span class="w"> </span><span class="nx">right</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-25-3" name="__codelineno-25-3" href="#__codelineno-25-3"></a><span class="w"> </span><span class="c1">// 使用了异或操作来简化代码</span> <a id="__codelineno-25-3" name="__codelineno-25-3" href="#__codelineno-25-3"></a><span class="w"> </span><span class="c1">// 此处使用异或运算来简化代码</span>
<a id="__codelineno-25-4" name="__codelineno-25-4" href="#__codelineno-25-4"></a><span class="w"> </span><span class="c1">// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span> <a id="__codelineno-25-4" name="__codelineno-25-4" href="#__codelineno-25-4"></a><span class="w"> </span><span class="c1">// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span>
<a id="__codelineno-25-5" name="__codelineno-25-5" href="#__codelineno-25-5"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nb">Number</span><span class="p">(</span><span class="nx">nums</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">mid</span><span class="p">])</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="nb">Number</span><span class="p">(</span><span class="nx">nums</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">right</span><span class="p">]))</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-25-5" name="__codelineno-25-5" href="#__codelineno-25-5"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nb">Number</span><span class="p">(</span><span class="nx">nums</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">mid</span><span class="p">])</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="nb">Number</span><span class="p">(</span><span class="nx">nums</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">right</span><span class="p">]))</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-25-6" name="__codelineno-25-6" href="#__codelineno-25-6"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">left</span><span class="p">;</span> <a id="__codelineno-25-6" name="__codelineno-25-6" href="#__codelineno-25-6"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">left</span><span class="p">;</span>
@ -2257,7 +2257,7 @@
<div class="highlight"><span class="filename">quick_sort.cs</span><pre><span></span><code><a id="__codelineno-27-1" name="__codelineno-27-1" href="#__codelineno-27-1"></a><span class="cm">/* 选取三个元素的中位数 */</span> <div class="highlight"><span class="filename">quick_sort.cs</span><pre><span></span><code><a id="__codelineno-27-1" name="__codelineno-27-1" href="#__codelineno-27-1"></a><span class="cm">/* 选取三个元素的中位数 */</span>
<a id="__codelineno-27-2" name="__codelineno-27-2" href="#__codelineno-27-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">medianThree</span><span class="p">(</span><span class="kt">int</span><span class="p">[]</span><span class="w"> </span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">left</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">mid</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">right</span><span class="p">)</span> <a id="__codelineno-27-2" name="__codelineno-27-2" href="#__codelineno-27-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">medianThree</span><span class="p">(</span><span class="kt">int</span><span class="p">[]</span><span class="w"> </span><span class="n">nums</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">left</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">mid</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">right</span><span class="p">)</span>
<a id="__codelineno-27-3" name="__codelineno-27-3" href="#__codelineno-27-3"></a><span class="p">{</span> <a id="__codelineno-27-3" name="__codelineno-27-3" href="#__codelineno-27-3"></a><span class="p">{</span>
<a id="__codelineno-27-4" name="__codelineno-27-4" href="#__codelineno-27-4"></a><span class="w"> </span><span class="c1">// 使用了异或操作来简化代码</span> <a id="__codelineno-27-4" name="__codelineno-27-4" href="#__codelineno-27-4"></a><span class="w"> </span><span class="c1">// 此处使用异或运算来简化代码</span>
<a id="__codelineno-27-5" name="__codelineno-27-5" href="#__codelineno-27-5"></a><span class="w"> </span><span class="c1">// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span> <a id="__codelineno-27-5" name="__codelineno-27-5" href="#__codelineno-27-5"></a><span class="w"> </span><span class="c1">// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span>
<a id="__codelineno-27-6" name="__codelineno-27-6" href="#__codelineno-27-6"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">((</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">mid</span><span class="p">])</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="p">(</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">right</span><span class="p">]))</span> <a id="__codelineno-27-6" name="__codelineno-27-6" href="#__codelineno-27-6"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">((</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">mid</span><span class="p">])</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="p">(</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">right</span><span class="p">]))</span>
<a id="__codelineno-27-7" name="__codelineno-27-7" href="#__codelineno-27-7"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">left</span><span class="p">;</span> <a id="__codelineno-27-7" name="__codelineno-27-7" href="#__codelineno-27-7"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">left</span><span class="p">;</span>
@ -2314,7 +2314,7 @@
<div class="tabbed-block"> <div class="tabbed-block">
<div class="highlight"><span class="filename">quick_sort.zig</span><pre><span></span><code><a id="__codelineno-29-1" name="__codelineno-29-1" href="#__codelineno-29-1"></a><span class="c1">// 选取三个元素的中位数</span> <div class="highlight"><span class="filename">quick_sort.zig</span><pre><span></span><code><a id="__codelineno-29-1" name="__codelineno-29-1" href="#__codelineno-29-1"></a><span class="c1">// 选取三个元素的中位数</span>
<a id="__codelineno-29-2" name="__codelineno-29-2" href="#__codelineno-29-2"></a><span class="k">fn</span><span class="w"> </span><span class="n">medianThree</span><span class="p">(</span><span class="n">nums</span><span class="o">:</span><span class="w"> </span><span class="p">[]</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="n">left</span><span class="o">:</span><span class="w"> </span><span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="n">mid</span><span class="o">:</span><span class="w"> </span><span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="n">right</span><span class="o">:</span><span class="w"> </span><span class="kt">usize</span><span class="p">)</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-29-2" name="__codelineno-29-2" href="#__codelineno-29-2"></a><span class="k">fn</span><span class="w"> </span><span class="n">medianThree</span><span class="p">(</span><span class="n">nums</span><span class="o">:</span><span class="w"> </span><span class="p">[]</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="n">left</span><span class="o">:</span><span class="w"> </span><span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="n">mid</span><span class="o">:</span><span class="w"> </span><span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="n">right</span><span class="o">:</span><span class="w"> </span><span class="kt">usize</span><span class="p">)</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-29-3" name="__codelineno-29-3" href="#__codelineno-29-3"></a><span class="w"> </span><span class="c1">// 使用了异或操作来简化代码</span> <a id="__codelineno-29-3" name="__codelineno-29-3" href="#__codelineno-29-3"></a><span class="w"> </span><span class="c1">// 此处使用异或运算来简化代码</span>
<a id="__codelineno-29-4" name="__codelineno-29-4" href="#__codelineno-29-4"></a><span class="w"> </span><span class="c1">// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span> <a id="__codelineno-29-4" name="__codelineno-29-4" href="#__codelineno-29-4"></a><span class="w"> </span><span class="c1">// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1</span>
<a id="__codelineno-29-5" name="__codelineno-29-5" href="#__codelineno-29-5"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">((</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">mid</span><span class="p">])</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="p">(</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">right</span><span class="p">]))</span><span class="w"> </span><span class="p">{</span> <a id="__codelineno-29-5" name="__codelineno-29-5" href="#__codelineno-29-5"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">((</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">mid</span><span class="p">])</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="p">(</span><span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="n">right</span><span class="p">]))</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-29-6" name="__codelineno-29-6" href="#__codelineno-29-6"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">left</span><span class="p">;</span> <a id="__codelineno-29-6" name="__codelineno-29-6" href="#__codelineno-29-6"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">left</span><span class="p">;</span>

File diff suppressed because one or more lines are too long

View file

@ -2,232 +2,232 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url> <url>
<loc>https://www.hello-algo.com/</loc> <loc>https://www.hello-algo.com/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/array/</loc> <loc>https://www.hello-algo.com/chapter_array_and_linkedlist/array/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/linked_list/</loc> <loc>https://www.hello-algo.com/chapter_array_and_linkedlist/linked_list/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/list/</loc> <loc>https://www.hello-algo.com/chapter_array_and_linkedlist/list/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/summary/</loc> <loc>https://www.hello-algo.com/chapter_array_and_linkedlist/summary/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_computational_complexity/performance_evaluation/</loc> <loc>https://www.hello-algo.com/chapter_computational_complexity/performance_evaluation/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_computational_complexity/space_complexity/</loc> <loc>https://www.hello-algo.com/chapter_computational_complexity/space_complexity/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_computational_complexity/space_time_tradeoff/</loc> <loc>https://www.hello-algo.com/chapter_computational_complexity/space_time_tradeoff/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_computational_complexity/summary/</loc> <loc>https://www.hello-algo.com/chapter_computational_complexity/summary/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_computational_complexity/time_complexity/</loc> <loc>https://www.hello-algo.com/chapter_computational_complexity/time_complexity/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_data_structure/classification_of_data_structure/</loc> <loc>https://www.hello-algo.com/chapter_data_structure/classification_of_data_structure/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_data_structure/data_and_memory/</loc> <loc>https://www.hello-algo.com/chapter_data_structure/data_and_memory/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_data_structure/summary/</loc> <loc>https://www.hello-algo.com/chapter_data_structure/summary/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_graph/graph/</loc> <loc>https://www.hello-algo.com/chapter_graph/graph/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_graph/graph_operations/</loc> <loc>https://www.hello-algo.com/chapter_graph/graph_operations/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_graph/graph_traversal/</loc> <loc>https://www.hello-algo.com/chapter_graph/graph_traversal/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_hashing/hash_collision/</loc> <loc>https://www.hello-algo.com/chapter_hashing/hash_collision/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_hashing/hash_map/</loc> <loc>https://www.hello-algo.com/chapter_hashing/hash_map/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_hashing/summary/</loc> <loc>https://www.hello-algo.com/chapter_hashing/summary/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_heap/heap/</loc> <loc>https://www.hello-algo.com/chapter_heap/heap/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_introduction/algorithms_are_everywhere/</loc> <loc>https://www.hello-algo.com/chapter_introduction/algorithms_are_everywhere/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_introduction/what_is_dsa/</loc> <loc>https://www.hello-algo.com/chapter_introduction/what_is_dsa/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_preface/about_the_book/</loc> <loc>https://www.hello-algo.com/chapter_preface/about_the_book/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_preface/contribution/</loc> <loc>https://www.hello-algo.com/chapter_preface/contribution/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_preface/installation/</loc> <loc>https://www.hello-algo.com/chapter_preface/installation/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_preface/suggestions/</loc> <loc>https://www.hello-algo.com/chapter_preface/suggestions/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_reference/</loc> <loc>https://www.hello-algo.com/chapter_reference/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_searching/binary_search/</loc> <loc>https://www.hello-algo.com/chapter_searching/binary_search/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_searching/hashing_search/</loc> <loc>https://www.hello-algo.com/chapter_searching/hashing_search/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_searching/linear_search/</loc> <loc>https://www.hello-algo.com/chapter_searching/linear_search/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_searching/summary/</loc> <loc>https://www.hello-algo.com/chapter_searching/summary/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_sorting/bubble_sort/</loc> <loc>https://www.hello-algo.com/chapter_sorting/bubble_sort/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_sorting/insertion_sort/</loc> <loc>https://www.hello-algo.com/chapter_sorting/insertion_sort/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_sorting/intro_to_sort/</loc> <loc>https://www.hello-algo.com/chapter_sorting/intro_to_sort/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_sorting/merge_sort/</loc> <loc>https://www.hello-algo.com/chapter_sorting/merge_sort/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_sorting/quick_sort/</loc> <loc>https://www.hello-algo.com/chapter_sorting/quick_sort/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_sorting/summary/</loc> <loc>https://www.hello-algo.com/chapter_sorting/summary/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_stack_and_queue/deque/</loc> <loc>https://www.hello-algo.com/chapter_stack_and_queue/deque/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_stack_and_queue/queue/</loc> <loc>https://www.hello-algo.com/chapter_stack_and_queue/queue/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_stack_and_queue/stack/</loc> <loc>https://www.hello-algo.com/chapter_stack_and_queue/stack/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_stack_and_queue/summary/</loc> <loc>https://www.hello-algo.com/chapter_stack_and_queue/summary/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_tree/avl_tree/</loc> <loc>https://www.hello-algo.com/chapter_tree/avl_tree/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_tree/binary_search_tree/</loc> <loc>https://www.hello-algo.com/chapter_tree/binary_search_tree/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_tree/binary_tree/</loc> <loc>https://www.hello-algo.com/chapter_tree/binary_tree/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_tree/binary_tree_traversal/</loc> <loc>https://www.hello-algo.com/chapter_tree/binary_tree_traversal/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
<url> <url>
<loc>https://www.hello-algo.com/chapter_tree/summary/</loc> <loc>https://www.hello-algo.com/chapter_tree/summary/</loc>
<lastmod>2023-02-18</lastmod> <lastmod>2023-02-21</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
</url> </url>
</urlset> </urlset>

Binary file not shown.