mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 02:26:28 +08:00
deploy
This commit is contained in:
parent
f31f630655
commit
9a0252f484
19 changed files with 137 additions and 135 deletions
|
@ -2246,12 +2246,12 @@
|
|||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">list.py</span><pre><span></span><code><a id="__codelineno-32-1" name="__codelineno-32-1" href="#__codelineno-32-1"></a><span class="c1"># 通过索引遍历列表</span>
|
||||
<a id="__codelineno-32-2" name="__codelineno-32-2" href="#__codelineno-32-2"></a><span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-32-2" name="__codelineno-32-2" href="#__codelineno-32-2"></a><span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-32-3" name="__codelineno-32-3" href="#__codelineno-32-3"></a><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">)):</span>
|
||||
<a id="__codelineno-32-4" name="__codelineno-32-4" href="#__codelineno-32-4"></a> <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
|
||||
<a id="__codelineno-32-5" name="__codelineno-32-5" href="#__codelineno-32-5"></a>
|
||||
<a id="__codelineno-32-6" name="__codelineno-32-6" href="#__codelineno-32-6"></a><span class="c1"># 直接遍历列表元素</span>
|
||||
<a id="__codelineno-32-7" name="__codelineno-32-7" href="#__codelineno-32-7"></a><span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-32-7" name="__codelineno-32-7" href="#__codelineno-32-7"></a><span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-32-8" name="__codelineno-32-8" href="#__codelineno-32-8"></a><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">:</span>
|
||||
<a id="__codelineno-32-9" name="__codelineno-32-9" href="#__codelineno-32-9"></a> <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
|
||||
</code></pre></div>
|
||||
|
|
|
@ -2836,23 +2836,25 @@
|
|||
<a id="__codelineno-42-18" name="__codelineno-42-18" href="#__codelineno-42-18"></a><span class="w"> </span><span class="sd">"""恢复状态"""</span>
|
||||
<a id="__codelineno-42-19" name="__codelineno-42-19" href="#__codelineno-42-19"></a> <span class="n">state</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
|
||||
<a id="__codelineno-42-20" name="__codelineno-42-20" href="#__codelineno-42-20"></a>
|
||||
<a id="__codelineno-42-21" name="__codelineno-42-21" href="#__codelineno-42-21"></a><span class="k">def</span> <span class="nf">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="n">TreeNode</span><span class="p">],</span> <span class="n">choices</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="n">TreeNode</span><span class="p">],</span> <span class="n">res</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">list</span><span class="p">[</span><span class="n">TreeNode</span><span class="p">]]):</span>
|
||||
<a id="__codelineno-42-22" name="__codelineno-42-22" href="#__codelineno-42-22"></a><span class="w"> </span><span class="sd">"""回溯算法:例题三"""</span>
|
||||
<a id="__codelineno-42-23" name="__codelineno-42-23" href="#__codelineno-42-23"></a> <span class="c1"># 检查是否为解</span>
|
||||
<a id="__codelineno-42-24" name="__codelineno-42-24" href="#__codelineno-42-24"></a> <span class="k">if</span> <span class="n">is_solution</span><span class="p">(</span><span class="n">state</span><span class="p">):</span>
|
||||
<a id="__codelineno-42-25" name="__codelineno-42-25" href="#__codelineno-42-25"></a> <span class="c1"># 记录解</span>
|
||||
<a id="__codelineno-42-26" name="__codelineno-42-26" href="#__codelineno-42-26"></a> <span class="n">record_solution</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">res</span><span class="p">)</span>
|
||||
<a id="__codelineno-42-27" name="__codelineno-42-27" href="#__codelineno-42-27"></a> <span class="k">return</span>
|
||||
<a id="__codelineno-42-28" name="__codelineno-42-28" href="#__codelineno-42-28"></a> <span class="c1"># 遍历所有选择</span>
|
||||
<a id="__codelineno-42-29" name="__codelineno-42-29" href="#__codelineno-42-29"></a> <span class="k">for</span> <span class="n">choice</span> <span class="ow">in</span> <span class="n">choices</span><span class="p">:</span>
|
||||
<a id="__codelineno-42-30" name="__codelineno-42-30" href="#__codelineno-42-30"></a> <span class="c1"># 剪枝:检查选择是否合法</span>
|
||||
<a id="__codelineno-42-31" name="__codelineno-42-31" href="#__codelineno-42-31"></a> <span class="k">if</span> <span class="n">is_valid</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">choice</span><span class="p">):</span>
|
||||
<a id="__codelineno-42-32" name="__codelineno-42-32" href="#__codelineno-42-32"></a> <span class="c1"># 尝试:做出选择,更新状态</span>
|
||||
<a id="__codelineno-42-33" name="__codelineno-42-33" href="#__codelineno-42-33"></a> <span class="n">make_choice</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">choice</span><span class="p">)</span>
|
||||
<a id="__codelineno-42-34" name="__codelineno-42-34" href="#__codelineno-42-34"></a> <span class="c1"># 进行下一轮选择</span>
|
||||
<a id="__codelineno-42-35" name="__codelineno-42-35" href="#__codelineno-42-35"></a> <span class="n">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="p">[</span><span class="n">choice</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">choice</span><span class="o">.</span><span class="n">right</span><span class="p">],</span> <span class="n">res</span><span class="p">)</span>
|
||||
<a id="__codelineno-42-36" name="__codelineno-42-36" href="#__codelineno-42-36"></a> <span class="c1"># 回退:撤销选择,恢复到之前的状态</span>
|
||||
<a id="__codelineno-42-37" name="__codelineno-42-37" href="#__codelineno-42-37"></a> <span class="n">undo_choice</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">choice</span><span class="p">)</span>
|
||||
<a id="__codelineno-42-21" name="__codelineno-42-21" href="#__codelineno-42-21"></a><span class="k">def</span> <span class="nf">backtrack</span><span class="p">(</span>
|
||||
<a id="__codelineno-42-22" name="__codelineno-42-22" href="#__codelineno-42-22"></a> <span class="n">state</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="n">TreeNode</span><span class="p">],</span> <span class="n">choices</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="n">TreeNode</span><span class="p">],</span> <span class="n">res</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">list</span><span class="p">[</span><span class="n">TreeNode</span><span class="p">]]</span>
|
||||
<a id="__codelineno-42-23" name="__codelineno-42-23" href="#__codelineno-42-23"></a><span class="p">):</span>
|
||||
<a id="__codelineno-42-24" name="__codelineno-42-24" href="#__codelineno-42-24"></a><span class="w"> </span><span class="sd">"""回溯算法:例题三"""</span>
|
||||
<a id="__codelineno-42-25" name="__codelineno-42-25" href="#__codelineno-42-25"></a> <span class="c1"># 检查是否为解</span>
|
||||
<a id="__codelineno-42-26" name="__codelineno-42-26" href="#__codelineno-42-26"></a> <span class="k">if</span> <span class="n">is_solution</span><span class="p">(</span><span class="n">state</span><span class="p">):</span>
|
||||
<a id="__codelineno-42-27" name="__codelineno-42-27" href="#__codelineno-42-27"></a> <span class="c1"># 记录解</span>
|
||||
<a id="__codelineno-42-28" name="__codelineno-42-28" href="#__codelineno-42-28"></a> <span class="n">record_solution</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">res</span><span class="p">)</span>
|
||||
<a id="__codelineno-42-29" name="__codelineno-42-29" href="#__codelineno-42-29"></a> <span class="k">return</span>
|
||||
<a id="__codelineno-42-30" name="__codelineno-42-30" href="#__codelineno-42-30"></a> <span class="c1"># 遍历所有选择</span>
|
||||
<a id="__codelineno-42-31" name="__codelineno-42-31" href="#__codelineno-42-31"></a> <span class="k">for</span> <span class="n">choice</span> <span class="ow">in</span> <span class="n">choices</span><span class="p">:</span>
|
||||
<a id="__codelineno-42-32" name="__codelineno-42-32" href="#__codelineno-42-32"></a> <span class="c1"># 剪枝:检查选择是否合法</span>
|
||||
<a id="__codelineno-42-33" name="__codelineno-42-33" href="#__codelineno-42-33"></a> <span class="k">if</span> <span class="n">is_valid</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">choice</span><span class="p">):</span>
|
||||
<a id="__codelineno-42-34" name="__codelineno-42-34" href="#__codelineno-42-34"></a> <span class="c1"># 尝试:做出选择,更新状态</span>
|
||||
<a id="__codelineno-42-35" name="__codelineno-42-35" href="#__codelineno-42-35"></a> <span class="n">make_choice</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">choice</span><span class="p">)</span>
|
||||
<a id="__codelineno-42-36" name="__codelineno-42-36" href="#__codelineno-42-36"></a> <span class="c1"># 进行下一轮选择</span>
|
||||
<a id="__codelineno-42-37" name="__codelineno-42-37" href="#__codelineno-42-37"></a> <span class="n">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="p">[</span><span class="n">choice</span><span class="o">.</span><span class="n">left</span><span class="p">,</span> <span class="n">choice</span><span class="o">.</span><span class="n">right</span><span class="p">],</span> <span class="n">res</span><span class="p">)</span>
|
||||
<a id="__codelineno-42-38" name="__codelineno-42-38" href="#__codelineno-42-38"></a> <span class="c1"># 回退:撤销选择,恢复到之前的状态</span>
|
||||
<a id="__codelineno-42-39" name="__codelineno-42-39" href="#__codelineno-42-39"></a> <span class="n">undo_choice</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">choice</span><span class="p">)</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
|
|
@ -2065,10 +2065,10 @@
|
|||
<a id="__codelineno-2-10" name="__codelineno-2-10" href="#__codelineno-2-10"></a> <span class="k">return</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-2-11" name="__codelineno-2-11" href="#__codelineno-2-11"></a>
|
||||
<a id="__codelineno-2-12" name="__codelineno-2-12" href="#__codelineno-2-12"></a><span class="k">def</span> <span class="nf">algorithm</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span> <span class="c1"># 输入数据</span>
|
||||
<a id="__codelineno-2-13" name="__codelineno-2-13" href="#__codelineno-2-13"></a> <span class="n">A</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># 暂存数据(常量,一般用大写字母表示)</span>
|
||||
<a id="__codelineno-2-14" name="__codelineno-2-14" href="#__codelineno-2-14"></a> <span class="n">b</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># 暂存数据(变量)</span>
|
||||
<a id="__codelineno-2-13" name="__codelineno-2-13" href="#__codelineno-2-13"></a> <span class="n">A</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># 暂存数据(常量,一般用大写字母表示)</span>
|
||||
<a id="__codelineno-2-14" name="__codelineno-2-14" href="#__codelineno-2-14"></a> <span class="n">b</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># 暂存数据(变量)</span>
|
||||
<a id="__codelineno-2-15" name="__codelineno-2-15" href="#__codelineno-2-15"></a> <span class="n">node</span> <span class="o">=</span> <span class="n">Node</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="c1"># 暂存数据(对象)</span>
|
||||
<a id="__codelineno-2-16" name="__codelineno-2-16" href="#__codelineno-2-16"></a> <span class="n">c</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">function</span><span class="p">()</span> <span class="c1"># 栈帧空间(调用函数)</span>
|
||||
<a id="__codelineno-2-16" name="__codelineno-2-16" href="#__codelineno-2-16"></a> <span class="n">c</span> <span class="o">=</span> <span class="n">function</span><span class="p">()</span> <span class="c1"># 栈帧空间(调用函数)</span>
|
||||
<a id="__codelineno-2-17" name="__codelineno-2-17" href="#__codelineno-2-17"></a> <span class="k">return</span> <span class="n">A</span> <span class="o">+</span> <span class="n">b</span> <span class="o">+</span> <span class="n">c</span> <span class="c1"># 输出数据</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
|
@ -2253,10 +2253,10 @@
|
|||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="k">def</span> <span class="nf">algorithm</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
|
||||
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a> <span class="n">a</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># O(1)</span>
|
||||
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a> <span class="n">b</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="mi">10000</span> <span class="c1"># O(1)</span>
|
||||
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a> <span class="n">a</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># O(1)</span>
|
||||
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a> <span class="n">b</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="mi">10000</span> <span class="c1"># O(1)</span>
|
||||
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a> <span class="k">if</span> <span class="n">n</span> <span class="o">></span> <span class="mi">10</span><span class="p">:</span>
|
||||
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a> <span class="n">nums</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span> <span class="c1"># O(n)</span>
|
||||
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a> <span class="n">nums</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span> <span class="c1"># O(n)</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -2572,12 +2572,12 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
|||
<div class="highlight"><span class="filename">space_complexity.py</span><pre><span></span><code><a id="__codelineno-32-1" name="__codelineno-32-1" href="#__codelineno-32-1"></a><span class="k">def</span> <span class="nf">constant</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
|
||||
<a id="__codelineno-32-2" name="__codelineno-32-2" href="#__codelineno-32-2"></a><span class="w"> </span><span class="sd">"""常数阶"""</span>
|
||||
<a id="__codelineno-32-3" name="__codelineno-32-3" href="#__codelineno-32-3"></a> <span class="c1"># 常量、变量、对象占用 O(1) 空间</span>
|
||||
<a id="__codelineno-32-4" name="__codelineno-32-4" href="#__codelineno-32-4"></a> <span class="n">a</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-32-5" name="__codelineno-32-5" href="#__codelineno-32-5"></a> <span class="n">nums</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="mi">10000</span>
|
||||
<a id="__codelineno-32-4" name="__codelineno-32-4" href="#__codelineno-32-4"></a> <span class="n">a</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-32-5" name="__codelineno-32-5" href="#__codelineno-32-5"></a> <span class="n">nums</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="mi">10000</span>
|
||||
<a id="__codelineno-32-6" name="__codelineno-32-6" href="#__codelineno-32-6"></a> <span class="n">node</span> <span class="o">=</span> <span class="n">ListNode</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
||||
<a id="__codelineno-32-7" name="__codelineno-32-7" href="#__codelineno-32-7"></a> <span class="c1"># 循环中的变量占用 O(1) 空间</span>
|
||||
<a id="__codelineno-32-8" name="__codelineno-32-8" href="#__codelineno-32-8"></a> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
||||
<a id="__codelineno-32-9" name="__codelineno-32-9" href="#__codelineno-32-9"></a> <span class="n">c</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-32-9" name="__codelineno-32-9" href="#__codelineno-32-9"></a> <span class="n">c</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-32-10" name="__codelineno-32-10" href="#__codelineno-32-10"></a> <span class="c1"># 循环中的函数占用 O(1) 空间</span>
|
||||
<a id="__codelineno-32-11" name="__codelineno-32-11" href="#__codelineno-32-11"></a> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
||||
<a id="__codelineno-32-12" name="__codelineno-32-12" href="#__codelineno-32-12"></a> <span class="n">function</span><span class="p">()</span>
|
||||
|
@ -2772,7 +2772,7 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
|||
<div class="highlight"><span class="filename">space_complexity.py</span><pre><span></span><code><a id="__codelineno-42-1" name="__codelineno-42-1" href="#__codelineno-42-1"></a><span class="k">def</span> <span class="nf">linear</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
|
||||
<a id="__codelineno-42-2" name="__codelineno-42-2" href="#__codelineno-42-2"></a><span class="w"> </span><span class="sd">"""线性阶"""</span>
|
||||
<a id="__codelineno-42-3" name="__codelineno-42-3" href="#__codelineno-42-3"></a> <span class="c1"># 长度为 n 的列表占用 O(n) 空间</span>
|
||||
<a id="__codelineno-42-4" name="__codelineno-42-4" href="#__codelineno-42-4"></a> <span class="n">nums</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span>
|
||||
<a id="__codelineno-42-4" name="__codelineno-42-4" href="#__codelineno-42-4"></a> <span class="n">nums</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span>
|
||||
<a id="__codelineno-42-5" name="__codelineno-42-5" href="#__codelineno-42-5"></a> <span class="c1"># 长度为 n 的哈希表占用 O(n) 空间</span>
|
||||
<a id="__codelineno-42-6" name="__codelineno-42-6" href="#__codelineno-42-6"></a> <span class="n">mapp</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">str</span><span class="p">]()</span>
|
||||
<a id="__codelineno-42-7" name="__codelineno-42-7" href="#__codelineno-42-7"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
||||
|
@ -3080,7 +3080,7 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
|||
<div class="highlight"><span class="filename">space_complexity.py</span><pre><span></span><code><a id="__codelineno-62-1" name="__codelineno-62-1" href="#__codelineno-62-1"></a><span class="k">def</span> <span class="nf">quadratic</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
|
||||
<a id="__codelineno-62-2" name="__codelineno-62-2" href="#__codelineno-62-2"></a><span class="w"> </span><span class="sd">"""平方阶"""</span>
|
||||
<a id="__codelineno-62-3" name="__codelineno-62-3" href="#__codelineno-62-3"></a> <span class="c1"># 二维列表占用 O(n^2) 空间</span>
|
||||
<a id="__codelineno-62-4" name="__codelineno-62-4" href="#__codelineno-62-4"></a> <span class="n">num_matrix</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]]</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)]</span>
|
||||
<a id="__codelineno-62-4" name="__codelineno-62-4" href="#__codelineno-62-4"></a> <span class="n">num_matrix</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)]</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
|
@ -3231,7 +3231,7 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
|||
<a id="__codelineno-72-3" name="__codelineno-72-3" href="#__codelineno-72-3"></a> <span class="k">if</span> <span class="n">n</span> <span class="o"><=</span> <span class="mi">0</span><span class="p">:</span>
|
||||
<a id="__codelineno-72-4" name="__codelineno-72-4" href="#__codelineno-72-4"></a> <span class="k">return</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-72-5" name="__codelineno-72-5" href="#__codelineno-72-5"></a> <span class="c1"># 数组 nums 长度为 n, n-1, ..., 2, 1</span>
|
||||
<a id="__codelineno-72-6" name="__codelineno-72-6" href="#__codelineno-72-6"></a> <span class="n">nums</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span>
|
||||
<a id="__codelineno-72-6" name="__codelineno-72-6" href="#__codelineno-72-6"></a> <span class="n">nums</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">n</span>
|
||||
<a id="__codelineno-72-7" name="__codelineno-72-7" href="#__codelineno-72-7"></a> <span class="k">return</span> <span class="n">quadratic_recur</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
|
||||
</code></pre></div>
|
||||
</div>
|
||||
|
|
|
@ -2447,7 +2447,7 @@ T(n) = 3 + 2n
|
|||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><pre><span></span><code><a id="__codelineno-22-1" name="__codelineno-22-1" href="#__codelineno-22-1"></a><span class="k">def</span> <span class="nf">algorithm</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
|
||||
<a id="__codelineno-22-2" name="__codelineno-22-2" href="#__codelineno-22-2"></a> <span class="n">a</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1"># +1</span>
|
||||
<a id="__codelineno-22-2" name="__codelineno-22-2" href="#__codelineno-22-2"></a> <span class="n">a</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1"># +1</span>
|
||||
<a id="__codelineno-22-3" name="__codelineno-22-3" href="#__codelineno-22-3"></a> <span class="n">a</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="mi">1</span> <span class="c1"># +1</span>
|
||||
<a id="__codelineno-22-4" name="__codelineno-22-4" href="#__codelineno-22-4"></a> <span class="n">a</span> <span class="o">=</span> <span class="n">a</span> <span class="o">*</span> <span class="mi">2</span> <span class="c1"># +1</span>
|
||||
<a id="__codelineno-22-5" name="__codelineno-22-5" href="#__codelineno-22-5"></a> <span class="c1"># 循环 n 次</span>
|
||||
|
@ -2610,8 +2610,8 @@ T(n) & = n^2 + n & \text{偷懒统计 (o.O)}
|
|||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><pre><span></span><code><a id="__codelineno-32-1" name="__codelineno-32-1" href="#__codelineno-32-1"></a><span class="k">def</span> <span class="nf">algorithm</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
|
||||
<a id="__codelineno-32-2" name="__codelineno-32-2" href="#__codelineno-32-2"></a> <span class="n">a</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1"># +0(技巧 1)</span>
|
||||
<a id="__codelineno-32-3" name="__codelineno-32-3" href="#__codelineno-32-3"></a> <span class="n">a</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">n</span> <span class="c1"># +0(技巧 1)</span>
|
||||
<a id="__codelineno-32-2" name="__codelineno-32-2" href="#__codelineno-32-2"></a> <span class="n">a</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1"># +0(技巧 1)</span>
|
||||
<a id="__codelineno-32-3" name="__codelineno-32-3" href="#__codelineno-32-3"></a> <span class="n">a</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">n</span> <span class="c1"># +0(技巧 1)</span>
|
||||
<a id="__codelineno-32-4" name="__codelineno-32-4" href="#__codelineno-32-4"></a> <span class="c1"># +n(技巧 2)</span>
|
||||
<a id="__codelineno-32-5" name="__codelineno-32-5" href="#__codelineno-32-5"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
|
||||
<a id="__codelineno-32-6" name="__codelineno-32-6" href="#__codelineno-32-6"></a> <span class="nb">print</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
||||
|
@ -2623,7 +2623,7 @@ T(n) & = n^2 + n & \text{偷懒统计 (o.O)}
|
|||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><pre><span></span><code><a id="__codelineno-33-1" name="__codelineno-33-1" href="#__codelineno-33-1"></a><span class="kd">func</span><span class="w"> </span><span class="nx">algorithm</span><span class="p">(</span><span class="nx">n</span><span class="w"> </span><span class="kt">int</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-33-2" name="__codelineno-33-2" href="#__codelineno-33-2"></a><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1">// +0(技巧 1)</span>
|
||||
<a id="__codelineno-33-2" name="__codelineno-33-2" href="#__codelineno-33-2"></a><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="c1">// +0(技巧 1)</span>
|
||||
<a id="__codelineno-33-3" name="__codelineno-33-3" href="#__codelineno-33-3"></a><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="c1">// +0(技巧 1)</span>
|
||||
<a id="__codelineno-33-4" name="__codelineno-33-4" href="#__codelineno-33-4"></a><span class="w"> </span><span class="c1">// +n(技巧 2)</span>
|
||||
<a id="__codelineno-33-5" name="__codelineno-33-5" href="#__codelineno-33-5"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="p"><</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="w"> </span><span class="p">{</span>
|
||||
|
@ -2813,8 +2813,8 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!
|
|||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">time_complexity.py</span><pre><span></span><code><a id="__codelineno-42-1" name="__codelineno-42-1" href="#__codelineno-42-1"></a><span class="k">def</span> <span class="nf">constant</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<a id="__codelineno-42-2" name="__codelineno-42-2" href="#__codelineno-42-2"></a><span class="w"> </span><span class="sd">"""常数阶"""</span>
|
||||
<a id="__codelineno-42-3" name="__codelineno-42-3" href="#__codelineno-42-3"></a> <span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-42-4" name="__codelineno-42-4" href="#__codelineno-42-4"></a> <span class="n">size</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">100000</span>
|
||||
<a id="__codelineno-42-3" name="__codelineno-42-3" href="#__codelineno-42-3"></a> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-42-4" name="__codelineno-42-4" href="#__codelineno-42-4"></a> <span class="n">size</span> <span class="o">=</span> <span class="mi">100000</span>
|
||||
<a id="__codelineno-42-5" name="__codelineno-42-5" href="#__codelineno-42-5"></a> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">size</span><span class="p">):</span>
|
||||
<a id="__codelineno-42-6" name="__codelineno-42-6" href="#__codelineno-42-6"></a> <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
|
||||
<a id="__codelineno-42-7" name="__codelineno-42-7" href="#__codelineno-42-7"></a> <span class="k">return</span> <span class="n">count</span>
|
||||
|
@ -2931,7 +2931,7 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!
|
|||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">time_complexity.py</span><pre><span></span><code><a id="__codelineno-52-1" name="__codelineno-52-1" href="#__codelineno-52-1"></a><span class="k">def</span> <span class="nf">linear</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<a id="__codelineno-52-2" name="__codelineno-52-2" href="#__codelineno-52-2"></a><span class="w"> </span><span class="sd">"""线性阶"""</span>
|
||||
<a id="__codelineno-52-3" name="__codelineno-52-3" href="#__codelineno-52-3"></a> <span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-52-3" name="__codelineno-52-3" href="#__codelineno-52-3"></a> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-52-4" name="__codelineno-52-4" href="#__codelineno-52-4"></a> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
||||
<a id="__codelineno-52-5" name="__codelineno-52-5" href="#__codelineno-52-5"></a> <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
|
||||
<a id="__codelineno-52-6" name="__codelineno-52-6" href="#__codelineno-52-6"></a> <span class="k">return</span> <span class="n">count</span>
|
||||
|
@ -3046,7 +3046,7 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!
|
|||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">time_complexity.py</span><pre><span></span><code><a id="__codelineno-62-1" name="__codelineno-62-1" href="#__codelineno-62-1"></a><span class="k">def</span> <span class="nf">array_traversal</span><span class="p">(</span><span class="n">nums</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">])</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<a id="__codelineno-62-2" name="__codelineno-62-2" href="#__codelineno-62-2"></a><span class="w"> </span><span class="sd">"""线性阶(遍历数组)"""</span>
|
||||
<a id="__codelineno-62-3" name="__codelineno-62-3" href="#__codelineno-62-3"></a> <span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-62-3" name="__codelineno-62-3" href="#__codelineno-62-3"></a> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-62-4" name="__codelineno-62-4" href="#__codelineno-62-4"></a> <span class="c1"># 循环次数与数组长度成正比</span>
|
||||
<a id="__codelineno-62-5" name="__codelineno-62-5" href="#__codelineno-62-5"></a> <span class="k">for</span> <span class="n">num</span> <span class="ow">in</span> <span class="n">nums</span><span class="p">:</span>
|
||||
<a id="__codelineno-62-6" name="__codelineno-62-6" href="#__codelineno-62-6"></a> <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
|
||||
|
@ -3174,7 +3174,7 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!
|
|||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">time_complexity.py</span><pre><span></span><code><a id="__codelineno-72-1" name="__codelineno-72-1" href="#__codelineno-72-1"></a><span class="k">def</span> <span class="nf">quadratic</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<a id="__codelineno-72-2" name="__codelineno-72-2" href="#__codelineno-72-2"></a><span class="w"> </span><span class="sd">"""平方阶"""</span>
|
||||
<a id="__codelineno-72-3" name="__codelineno-72-3" href="#__codelineno-72-3"></a> <span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-72-3" name="__codelineno-72-3" href="#__codelineno-72-3"></a> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-72-4" name="__codelineno-72-4" href="#__codelineno-72-4"></a> <span class="c1"># 循环次数与数组长度成平方关系</span>
|
||||
<a id="__codelineno-72-5" name="__codelineno-72-5" href="#__codelineno-72-5"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
||||
<a id="__codelineno-72-6" name="__codelineno-72-6" href="#__codelineno-72-6"></a> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
||||
|
@ -3338,7 +3338,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
|||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">time_complexity.py</span><pre><span></span><code><a id="__codelineno-82-1" name="__codelineno-82-1" href="#__codelineno-82-1"></a><span class="k">def</span> <span class="nf">bubble_sort</span><span class="p">(</span><span class="n">nums</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">])</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<a id="__codelineno-82-2" name="__codelineno-82-2" href="#__codelineno-82-2"></a><span class="w"> </span><span class="sd">"""平方阶(冒泡排序)"""</span>
|
||||
<a id="__codelineno-82-3" name="__codelineno-82-3" href="#__codelineno-82-3"></a> <span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># 计数器</span>
|
||||
<a id="__codelineno-82-3" name="__codelineno-82-3" href="#__codelineno-82-3"></a> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># 计数器</span>
|
||||
<a id="__codelineno-82-4" name="__codelineno-82-4" href="#__codelineno-82-4"></a> <span class="c1"># 外循环:待排序元素数量为 n-1, n-2, ..., 1</span>
|
||||
<a id="__codelineno-82-5" name="__codelineno-82-5" href="#__codelineno-82-5"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">nums</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">):</span>
|
||||
<a id="__codelineno-82-6" name="__codelineno-82-6" href="#__codelineno-82-6"></a> <span class="c1"># 内循环:冒泡操作</span>
|
||||
|
@ -3546,8 +3546,8 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
|||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">time_complexity.py</span><pre><span></span><code><a id="__codelineno-92-1" name="__codelineno-92-1" href="#__codelineno-92-1"></a><span class="k">def</span> <span class="nf">exponential</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<a id="__codelineno-92-2" name="__codelineno-92-2" href="#__codelineno-92-2"></a><span class="w"> </span><span class="sd">"""指数阶(循环实现)"""</span>
|
||||
<a id="__codelineno-92-3" name="__codelineno-92-3" href="#__codelineno-92-3"></a> <span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-92-4" name="__codelineno-92-4" href="#__codelineno-92-4"></a> <span class="n">base</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span>
|
||||
<a id="__codelineno-92-3" name="__codelineno-92-3" href="#__codelineno-92-3"></a> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-92-4" name="__codelineno-92-4" href="#__codelineno-92-4"></a> <span class="n">base</span> <span class="o">=</span> <span class="mi">1</span>
|
||||
<a id="__codelineno-92-5" name="__codelineno-92-5" href="#__codelineno-92-5"></a> <span class="c1"># cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
||||
<a id="__codelineno-92-6" name="__codelineno-92-6" href="#__codelineno-92-6"></a> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
||||
<a id="__codelineno-92-7" name="__codelineno-92-7" href="#__codelineno-92-7"></a> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">base</span><span class="p">):</span>
|
||||
|
@ -3806,7 +3806,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
|||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">time_complexity.py</span><pre><span></span><code><a id="__codelineno-112-1" name="__codelineno-112-1" href="#__codelineno-112-1"></a><span class="k">def</span> <span class="nf">logarithmic</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">float</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<a id="__codelineno-112-2" name="__codelineno-112-2" href="#__codelineno-112-2"></a><span class="w"> </span><span class="sd">"""对数阶(循环实现)"""</span>
|
||||
<a id="__codelineno-112-3" name="__codelineno-112-3" href="#__codelineno-112-3"></a> <span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-112-3" name="__codelineno-112-3" href="#__codelineno-112-3"></a> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-112-4" name="__codelineno-112-4" href="#__codelineno-112-4"></a> <span class="k">while</span> <span class="n">n</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span>
|
||||
<a id="__codelineno-112-5" name="__codelineno-112-5" href="#__codelineno-112-5"></a> <span class="n">n</span> <span class="o">=</span> <span class="n">n</span> <span class="o">/</span> <span class="mi">2</span>
|
||||
<a id="__codelineno-112-6" name="__codelineno-112-6" href="#__codelineno-112-6"></a> <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
|
||||
|
@ -4179,7 +4179,7 @@ n! = n \times (n - 1) \times (n - 2) \times \cdots \times 2 \times 1
|
|||
<a id="__codelineno-142-2" name="__codelineno-142-2" href="#__codelineno-142-2"></a><span class="w"> </span><span class="sd">"""阶乘阶(递归实现)"""</span>
|
||||
<a id="__codelineno-142-3" name="__codelineno-142-3" href="#__codelineno-142-3"></a> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
|
||||
<a id="__codelineno-142-4" name="__codelineno-142-4" href="#__codelineno-142-4"></a> <span class="k">return</span> <span class="mi">1</span>
|
||||
<a id="__codelineno-142-5" name="__codelineno-142-5" href="#__codelineno-142-5"></a> <span class="n">count</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-142-5" name="__codelineno-142-5" href="#__codelineno-142-5"></a> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-142-6" name="__codelineno-142-6" href="#__codelineno-142-6"></a> <span class="c1"># 从 1 个分裂出 n 个</span>
|
||||
<a id="__codelineno-142-7" name="__codelineno-142-7" href="#__codelineno-142-7"></a> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
||||
<a id="__codelineno-142-8" name="__codelineno-142-8" href="#__codelineno-142-8"></a> <span class="n">count</span> <span class="o">+=</span> <span class="n">factorial_recur</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
|
||||
|
@ -4357,7 +4357,7 @@ n! = n \times (n - 1) \times (n - 2) \times \cdots \times 2 \times 1
|
|||
<div class="highlight"><span class="filename">worst_best_time_complexity.py</span><pre><span></span><code><a id="__codelineno-152-1" name="__codelineno-152-1" href="#__codelineno-152-1"></a><span class="k">def</span> <span class="nf">random_numbers</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]:</span>
|
||||
<a id="__codelineno-152-2" name="__codelineno-152-2" href="#__codelineno-152-2"></a><span class="w"> </span><span class="sd">"""生成一个数组,元素为: 1, 2, ..., n ,顺序被打乱"""</span>
|
||||
<a id="__codelineno-152-3" name="__codelineno-152-3" href="#__codelineno-152-3"></a> <span class="c1"># 生成数组 nums =: 1, 2, 3, ..., n</span>
|
||||
<a id="__codelineno-152-4" name="__codelineno-152-4" href="#__codelineno-152-4"></a> <span class="n">nums</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]</span>
|
||||
<a id="__codelineno-152-4" name="__codelineno-152-4" href="#__codelineno-152-4"></a> <span class="n">nums</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]</span>
|
||||
<a id="__codelineno-152-5" name="__codelineno-152-5" href="#__codelineno-152-5"></a> <span class="c1"># 随机打乱数组元素</span>
|
||||
<a id="__codelineno-152-6" name="__codelineno-152-6" href="#__codelineno-152-6"></a> <span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">nums</span><span class="p">)</span>
|
||||
<a id="__codelineno-152-7" name="__codelineno-152-7" href="#__codelineno-152-7"></a> <span class="k">return</span> <span class="n">nums</span>
|
||||
|
|
|
@ -1929,7 +1929,7 @@
|
|||
<p>然而,GB2312 无法处理部分的罕见字和繁体字。之后在 GB2312 的基础上,扩展得到了「GBK」字符集,它共收录了 21886 个汉字。在 GBK 编码方案中,ASCII 字符使用一个字节表示,汉字使用两个字节表示。</p>
|
||||
<h2 id="343-unicode">3.4.3. Unicode 字符集<a class="headerlink" href="#343-unicode" title="Permanent link">¶</a></h2>
|
||||
<p>随着计算机的蓬勃发展,字符集与编码标准百花齐放,而这带来了许多问题。一方面,这些字符集一般只定义了特定语言的字符,无法在多语言环境下正常工作;另一方面,同一种语言也存在多种字符集标准,如果两台电脑安装的是不同的编码标准,则在信息传递时就会出现乱码。</p>
|
||||
<p>那个时代的人们就在想:如果推出一个足够完整的字符集,将世界范围内的所有语言和符号都纳入其中,不就可以解决跨语言环境和乱码问题了吗?在这种想法的驱动下,一个大而全的字符集 Unicode 应运而生。</p>
|
||||
<p>那个时代的人们就在想:<strong>如果推出一个足够完整的字符集,将世界范围内的所有语言和符号都收录其中,不就可以解决跨语言环境和乱码问题了吗</strong>?在这种想法的驱动下,一个大而全的字符集 Unicode 应运而生。</p>
|
||||
<p>「Unicode」的全称为“统一字符编码”,理论上能容纳一百多万个字符。它致力于将全球范围内的字符纳入到统一的字符集之中,提供一种通用的字符集来处理和显示各种语言文字,减少因为编码标准不同而产生的乱码问题。</p>
|
||||
<p>自 1991 年发布以来,Unicode 不断扩充新的语言与字符。截止 2022 年 9 月,Unicode 已经包含 149186 个字符,包括各种语言的字符、符号、甚至是表情符号等。在庞大的 Unicode 字符集中,常用的字符占用 2 字节,有些生僻的字符占 3 字节甚至 4 字节。</p>
|
||||
<p>Unicode 是一种字符集标准,本质上是给每个字符分配一个编号(称为“码点”),<strong>但它并没有规定在计算机中如何存储这些字符码点</strong>。我们不禁会问:当多种长度的 Unicode 码点同时出现在同一个文本中时,系统如何解析字符?例如,给定一个长度为 2 字节的编码,系统如何确认它是一个 2 字节的字符还是两个 1 字节的字符?</p>
|
||||
|
@ -1942,7 +1942,7 @@
|
|||
<p>目前,UTF-8 已成为国际上使用最广泛的 Unicode 编码方法。<strong>它是一种可变长的编码</strong>,使用 1 到 4 个字节来表示一个字符,根据字符的复杂性而变。ASCII 字符只需要 1 个字节,拉丁字母和希腊字母需要 2 个字节,常用的中文字符需要 3 个字节,其他的一些生僻字符需要 4 个字节。</p>
|
||||
<p>UTF-8 的编码规则并不复杂,分为两种情况:</p>
|
||||
<ul>
|
||||
<li><strong>长度为 1 字节的字符</strong>:将最高位设置为 0 、其余 7 位设置为 Unicode 码点。值得注意的是,ASCII 字符在 Unicode 字符集中占据了前 128 个码点。也就是说,<strong>UTF-8 编码可以向下兼容 ASCII 码</strong>。这意味着我们可以使用 UTF-8 来解析年代久远的 ASCII 码文本。</li>
|
||||
<li>对于长度为 1 字节的字符,将最高位设置为 <span class="arithmatex">\(0\)</span> 、其余 7 位设置为 Unicode 码点。值得注意的是,ASCII 字符在 Unicode 字符集中占据了前 128 个码点。也就是说,<strong>UTF-8 编码可以向下兼容 ASCII 码</strong>。这意味着我们可以使用 UTF-8 来解析年代久远的 ASCII 码文本。</li>
|
||||
<li>对于长度为 <span class="arithmatex">\(n\)</span> 字节的字符(其中 <span class="arithmatex">\(n > 1\)</span>),将首个字节的高 <span class="arithmatex">\(n\)</span> 位都设置为 <span class="arithmatex">\(1\)</span> 、第 <span class="arithmatex">\(n + 1\)</span> 位设置为 <span class="arithmatex">\(0\)</span> ;从第二个字节开始,将每个字节的高 2 位都设置为 <span class="arithmatex">\(10\)</span> ;其余所有位用于填充字符的 Unicode 码点。</li>
|
||||
</ul>
|
||||
<p>下图展示了“Hello算法”对应的 UTF-8 编码。将最高 <span class="arithmatex">\(n\)</span> 位设置为 <span class="arithmatex">\(1\)</span> 比较容易理解,可以向系统指出字符的长度为 <span class="arithmatex">\(n\)</span> 。那么,为什么要将其余所有字节的高 2 位都设置为 <span class="arithmatex">\(10\)</span> 呢?实际上,这个 <span class="arithmatex">\(10\)</span> 能够起到校验符的作用,因为在 UTF-8 编码规则下,不可能有字符的最高两位是 <span class="arithmatex">\(10\)</span> 。这是因为长度为 1 字节的字符的最高一位是 <span class="arithmatex">\(0\)</span> 。假设系统从一个错误的字节开始解析文本,字节头部的 <span class="arithmatex">\(10\)</span> 能够帮助系统快速的判断出异常。</p>
|
||||
|
|
|
@ -2486,7 +2486,7 @@ f(x) = x \bmod {100}
|
|||
<a id="__codelineno-22-15" name="__codelineno-22-15" href="#__codelineno-22-15"></a>
|
||||
<a id="__codelineno-22-16" name="__codelineno-22-16" href="#__codelineno-22-16"></a> <span class="k">def</span> <span class="nf">hash_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<a id="__codelineno-22-17" name="__codelineno-22-17" href="#__codelineno-22-17"></a><span class="w"> </span><span class="sd">"""哈希函数"""</span>
|
||||
<a id="__codelineno-22-18" name="__codelineno-22-18" href="#__codelineno-22-18"></a> <span class="n">index</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">key</span> <span class="o">%</span> <span class="mi">100</span>
|
||||
<a id="__codelineno-22-18" name="__codelineno-22-18" href="#__codelineno-22-18"></a> <span class="n">index</span> <span class="o">=</span> <span class="n">key</span> <span class="o">%</span> <span class="mi">100</span>
|
||||
<a id="__codelineno-22-19" name="__codelineno-22-19" href="#__codelineno-22-19"></a> <span class="k">return</span> <span class="n">index</span>
|
||||
<a id="__codelineno-22-20" name="__codelineno-22-20" href="#__codelineno-22-20"></a>
|
||||
<a id="__codelineno-22-21" name="__codelineno-22-21" href="#__codelineno-22-21"></a> <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="nb">str</span><span class="p">:</span>
|
||||
|
@ -2519,7 +2519,7 @@ f(x) = x \bmod {100}
|
|||
<a id="__codelineno-22-48" name="__codelineno-22-48" href="#__codelineno-22-48"></a>
|
||||
<a id="__codelineno-22-49" name="__codelineno-22-49" href="#__codelineno-22-49"></a> <span class="k">def</span> <span class="nf">key_set</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]:</span>
|
||||
<a id="__codelineno-22-50" name="__codelineno-22-50" href="#__codelineno-22-50"></a><span class="w"> </span><span class="sd">"""获取所有键"""</span>
|
||||
<a id="__codelineno-22-51" name="__codelineno-22-51" href="#__codelineno-22-51"></a> <span class="n">result</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-22-51" name="__codelineno-22-51" href="#__codelineno-22-51"></a> <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-22-52" name="__codelineno-22-52" href="#__codelineno-22-52"></a> <span class="k">for</span> <span class="n">pair</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">buckets</span><span class="p">:</span>
|
||||
<a id="__codelineno-22-53" name="__codelineno-22-53" href="#__codelineno-22-53"></a> <span class="k">if</span> <span class="n">pair</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
|
||||
<a id="__codelineno-22-54" name="__codelineno-22-54" href="#__codelineno-22-54"></a> <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pair</span><span class="o">.</span><span class="n">key</span><span class="p">)</span>
|
||||
|
@ -2527,7 +2527,7 @@ f(x) = x \bmod {100}
|
|||
<a id="__codelineno-22-56" name="__codelineno-22-56" href="#__codelineno-22-56"></a>
|
||||
<a id="__codelineno-22-57" name="__codelineno-22-57" href="#__codelineno-22-57"></a> <span class="k">def</span> <span class="nf">value_set</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">list</span><span class="p">[</span><span class="nb">str</span><span class="p">]:</span>
|
||||
<a id="__codelineno-22-58" name="__codelineno-22-58" href="#__codelineno-22-58"></a><span class="w"> </span><span class="sd">"""获取所有值"""</span>
|
||||
<a id="__codelineno-22-59" name="__codelineno-22-59" href="#__codelineno-22-59"></a> <span class="n">result</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-22-59" name="__codelineno-22-59" href="#__codelineno-22-59"></a> <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-22-60" name="__codelineno-22-60" href="#__codelineno-22-60"></a> <span class="k">for</span> <span class="n">pair</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">buckets</span><span class="p">:</span>
|
||||
<a id="__codelineno-22-61" name="__codelineno-22-61" href="#__codelineno-22-61"></a> <span class="k">if</span> <span class="n">pair</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
|
||||
<a id="__codelineno-22-62" name="__codelineno-22-62" href="#__codelineno-22-62"></a> <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pair</span><span class="o">.</span><span class="n">val</span><span class="p">)</span>
|
||||
|
|
|
@ -1889,7 +1889,7 @@
|
|||
|
||||
|
||||
<h1 id="102">10.2. 二分查找边界<a class="headerlink" href="#102" title="Permanent link">¶</a></h1>
|
||||
<p>上一节规定目标元素在数组中是唯一的。如果目标元素在数组中多次出现,上节介绍的方法只能保证返回其中一个目标元素的索引,<strong>而无法确定该索引的左边和右边还有多少目标元素</strong>。</p>
|
||||
<p>在上一节中,题目规定数组中所有元素都是唯一的。如果目标元素在数组中多次出现,上节介绍的方法只能保证返回其中一个目标元素的索引,<strong>而无法确定该索引的左边和右边还有多少目标元素</strong>。</p>
|
||||
<div class="admonition question">
|
||||
<p class="admonition-title">Question</p>
|
||||
<p>给定一个长度为 <span class="arithmatex">\(n\)</span> 的有序数组 <code>nums</code> ,数组可能包含重复元素。请查找并返回元素 <code>target</code> 在数组中首次出现的索引。若数组中不包含该元素,则返回 <span class="arithmatex">\(-1\)</span> 。</p>
|
||||
|
|
|
@ -1890,7 +1890,7 @@
|
|||
|
||||
<h1 id="104">10.4. 搜索算法<a class="headerlink" href="#104" title="Permanent link">¶</a></h1>
|
||||
<p>「搜索算法 Searching Algorithm」用于在数据结构(例如数组、链表、树或图)中搜索一个或一组满足特定条件的元素。</p>
|
||||
<p>我们已经学过数组、链表、树和图的遍历方法,也学过哈希表、二叉搜索树等可用于实现查询的复杂数据结构。因此,搜索算法对于我们来说并不陌生。在本节,我们将从更加系统的视角切入,重新审视搜索算法。</p>
|
||||
<p>在前面的章节中,我们已经学习了数组、链表、树和图的遍历方法,也了解过哈希表和二叉搜索树等具有查询功能的复杂数据结构。因此,搜索算法对于我们来说并不陌生。在本节,我们将从更加系统的视角切入,重新审视搜索算法。</p>
|
||||
<h2 id="1041">10.4.1. 暴力搜索<a class="headerlink" href="#1041" title="Permanent link">¶</a></h2>
|
||||
<p>暴力搜索通过遍历数据结构的每个元素来定位目标元素。</p>
|
||||
<ul>
|
||||
|
|
|
@ -1967,7 +1967,7 @@
|
|||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">bubble_sort.py</span><pre><span></span><code><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="k">def</span> <span class="nf">bubble_sort</span><span class="p">(</span><span class="n">nums</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">])</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
|
||||
<a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="w"> </span><span class="sd">"""冒泡排序"""</span>
|
||||
<a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a> <span class="n">n</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">nums</span><span class="p">)</span>
|
||||
<a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">nums</span><span class="p">)</span>
|
||||
<a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a> <span class="c1"># 外循环:待排序元素数量为 n-1, n-2, ..., 1</span>
|
||||
<a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">):</span>
|
||||
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a> <span class="c1"># 内循环:冒泡操作</span>
|
||||
|
@ -2159,10 +2159,10 @@
|
|||
<div class="tabbed-block">
|
||||
<div class="highlight"><span class="filename">bubble_sort.py</span><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1" href="#__codelineno-12-1"></a><span class="k">def</span> <span class="nf">bubble_sort_with_flag</span><span class="p">(</span><span class="n">nums</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">])</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
|
||||
<a id="__codelineno-12-2" name="__codelineno-12-2" href="#__codelineno-12-2"></a><span class="w"> </span><span class="sd">"""冒泡排序(标志优化)"""</span>
|
||||
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a> <span class="n">n</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">nums</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-3" name="__codelineno-12-3" href="#__codelineno-12-3"></a> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">nums</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a> <span class="c1"># 外循环:待排序元素数量为 n-1, n-2, ..., 1</span>
|
||||
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">):</span>
|
||||
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a> <span class="n">flag</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span> <span class="c1"># 初始化标志位</span>
|
||||
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a> <span class="n">flag</span> <span class="o">=</span> <span class="kc">False</span> <span class="c1"># 初始化标志位</span>
|
||||
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a> <span class="c1"># 内循环:冒泡操作</span>
|
||||
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
|
||||
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a> <span class="k">if</span> <span class="n">nums</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">></span> <span class="n">nums</span><span class="p">[</span><span class="n">j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]:</span>
|
||||
|
|
|
@ -1943,8 +1943,8 @@
|
|||
<a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="w"> </span><span class="sd">"""插入排序"""</span>
|
||||
<a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a> <span class="c1"># 外循环:base = nums[1], nums[2], ..., nums[n-1]</span>
|
||||
<a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">nums</span><span class="p">)):</span>
|
||||
<a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a> <span class="n">base</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
|
||||
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a> <span class="n">j</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span>
|
||||
<a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a> <span class="n">base</span> <span class="o">=</span> <span class="n">nums</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
|
||||
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a> <span class="n">j</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span>
|
||||
<a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a> <span class="c1"># 内循环:将 base 插入到左边的正确位置</span>
|
||||
<a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a> <span class="k">while</span> <span class="n">j</span> <span class="o">>=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">nums</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">></span> <span class="n">base</span><span class="p">:</span>
|
||||
<a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a> <span class="n">nums</span><span class="p">[</span><span class="n">j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">nums</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="c1"># 1. 将 nums[j] 向右移动一位</span>
|
||||
|
|
|
@ -2033,16 +2033,16 @@
|
|||
<a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a> <span class="c1"># 左子数组区间 [left, mid]</span>
|
||||
<a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a> <span class="c1"># 右子数组区间 [mid + 1, right]</span>
|
||||
<a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a> <span class="c1"># 初始化辅助数组</span>
|
||||
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a> <span class="n">tmp</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="nb">list</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">right</span> <span class="o">+</span> <span class="mi">1</span><span class="p">])</span>
|
||||
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a> <span class="n">tmp</span> <span class="o">=</span> <span class="nb">list</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">right</span> <span class="o">+</span> <span class="mi">1</span><span class="p">])</span>
|
||||
<a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a> <span class="c1"># 左子数组的起始索引和结束索引</span>
|
||||
<a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a> <span class="n">left_start</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a> <span class="n">left_end</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">-</span> <span class="n">left</span>
|
||||
<a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a> <span class="n">left_start</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a> <span class="n">left_end</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">-</span> <span class="n">left</span>
|
||||
<a id="__codelineno-2-10" name="__codelineno-2-10" href="#__codelineno-2-10"></a> <span class="c1"># 右子数组的起始索引和结束索引</span>
|
||||
<a id="__codelineno-2-11" name="__codelineno-2-11" href="#__codelineno-2-11"></a> <span class="n">right_start</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">left</span>
|
||||
<a id="__codelineno-2-12" name="__codelineno-2-12" href="#__codelineno-2-12"></a> <span class="n">right_end</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">right</span> <span class="o">-</span> <span class="n">left</span>
|
||||
<a id="__codelineno-2-11" name="__codelineno-2-11" href="#__codelineno-2-11"></a> <span class="n">right_start</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">left</span>
|
||||
<a id="__codelineno-2-12" name="__codelineno-2-12" href="#__codelineno-2-12"></a> <span class="n">right_end</span> <span class="o">=</span> <span class="n">right</span> <span class="o">-</span> <span class="n">left</span>
|
||||
<a id="__codelineno-2-13" name="__codelineno-2-13" href="#__codelineno-2-13"></a> <span class="c1"># i, j 分别指向左子数组、右子数组的首元素</span>
|
||||
<a id="__codelineno-2-14" name="__codelineno-2-14" href="#__codelineno-2-14"></a> <span class="n">i</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">left_start</span>
|
||||
<a id="__codelineno-2-15" name="__codelineno-2-15" href="#__codelineno-2-15"></a> <span class="n">j</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">right_start</span>
|
||||
<a id="__codelineno-2-14" name="__codelineno-2-14" href="#__codelineno-2-14"></a> <span class="n">i</span> <span class="o">=</span> <span class="n">left_start</span>
|
||||
<a id="__codelineno-2-15" name="__codelineno-2-15" href="#__codelineno-2-15"></a> <span class="n">j</span> <span class="o">=</span> <span class="n">right_start</span>
|
||||
<a id="__codelineno-2-16" name="__codelineno-2-16" href="#__codelineno-2-16"></a> <span class="c1"># 通过覆盖原数组 nums 来合并左子数组和右子数组</span>
|
||||
<a id="__codelineno-2-17" name="__codelineno-2-17" href="#__codelineno-2-17"></a> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">left</span><span class="p">,</span> <span class="n">right</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
|
||||
<a id="__codelineno-2-18" name="__codelineno-2-18" href="#__codelineno-2-18"></a> <span class="c1"># 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++</span>
|
||||
|
@ -2064,7 +2064,7 @@
|
|||
<a id="__codelineno-2-34" name="__codelineno-2-34" href="#__codelineno-2-34"></a> <span class="k">if</span> <span class="n">left</span> <span class="o">>=</span> <span class="n">right</span><span class="p">:</span>
|
||||
<a id="__codelineno-2-35" name="__codelineno-2-35" href="#__codelineno-2-35"></a> <span class="k">return</span> <span class="c1"># 当子数组长度为 1 时终止递归</span>
|
||||
<a id="__codelineno-2-36" name="__codelineno-2-36" href="#__codelineno-2-36"></a> <span class="c1"># 划分阶段</span>
|
||||
<a id="__codelineno-2-37" name="__codelineno-2-37" href="#__codelineno-2-37"></a> <span class="n">mid</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="p">(</span><span class="n">left</span> <span class="o">+</span> <span class="n">right</span><span class="p">)</span> <span class="o">//</span> <span class="mi">2</span> <span class="c1"># 计算中点</span>
|
||||
<a id="__codelineno-2-37" name="__codelineno-2-37" href="#__codelineno-2-37"></a> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">left</span> <span class="o">+</span> <span class="n">right</span><span class="p">)</span> <span class="o">//</span> <span class="mi">2</span> <span class="c1"># 计算中点</span>
|
||||
<a id="__codelineno-2-38" name="__codelineno-2-38" href="#__codelineno-2-38"></a> <span class="n">merge_sort</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="c1"># 递归左子数组</span>
|
||||
<a id="__codelineno-2-39" name="__codelineno-2-39" href="#__codelineno-2-39"></a> <span class="n">merge_sort</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">right</span><span class="p">)</span> <span class="c1"># 递归右子数组</span>
|
||||
<a id="__codelineno-2-40" name="__codelineno-2-40" href="#__codelineno-2-40"></a> <span class="c1"># 合并阶段</span>
|
||||
|
|
|
@ -2258,7 +2258,7 @@
|
|||
<a id="__codelineno-12-4" name="__codelineno-12-4" href="#__codelineno-12-4"></a> <span class="k">if</span> <span class="n">left</span> <span class="o">>=</span> <span class="n">right</span><span class="p">:</span>
|
||||
<a id="__codelineno-12-5" name="__codelineno-12-5" href="#__codelineno-12-5"></a> <span class="k">return</span>
|
||||
<a id="__codelineno-12-6" name="__codelineno-12-6" href="#__codelineno-12-6"></a> <span class="c1"># 哨兵划分</span>
|
||||
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a> <span class="n">pivot</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partition</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">right</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-7" name="__codelineno-12-7" href="#__codelineno-12-7"></a> <span class="n">pivot</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partition</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">right</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-8" name="__codelineno-12-8" href="#__codelineno-12-8"></a> <span class="c1"># 递归左子数组、右子数组</span>
|
||||
<a id="__codelineno-12-9" name="__codelineno-12-9" href="#__codelineno-12-9"></a> <span class="bp">self</span><span class="o">.</span><span class="n">quick_sort</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">pivot</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
|
||||
<a id="__codelineno-12-10" name="__codelineno-12-10" href="#__codelineno-12-10"></a> <span class="bp">self</span><span class="o">.</span><span class="n">quick_sort</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span> <span class="n">pivot</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">right</span><span class="p">)</span>
|
||||
|
@ -2488,7 +2488,7 @@
|
|||
<a id="__codelineno-22-11" name="__codelineno-22-11" href="#__codelineno-22-11"></a><span class="k">def</span> <span class="nf">partition</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="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">],</span> <span class="n">left</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">right</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<a id="__codelineno-22-12" name="__codelineno-22-12" href="#__codelineno-22-12"></a><span class="w"> </span><span class="sd">"""哨兵划分(三数取中值)"""</span>
|
||||
<a id="__codelineno-22-13" name="__codelineno-22-13" href="#__codelineno-22-13"></a> <span class="c1"># 以 nums[left] 作为基准数</span>
|
||||
<a id="__codelineno-22-14" name="__codelineno-22-14" href="#__codelineno-22-14"></a> <span class="n">med</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">median_three</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="p">(</span><span class="n">left</span> <span class="o">+</span> <span class="n">right</span><span class="p">)</span> <span class="o">//</span> <span class="mi">2</span><span class="p">,</span> <span class="n">right</span><span class="p">)</span>
|
||||
<a id="__codelineno-22-14" name="__codelineno-22-14" href="#__codelineno-22-14"></a> <span class="n">med</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">median_three</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="p">(</span><span class="n">left</span> <span class="o">+</span> <span class="n">right</span><span class="p">)</span> <span class="o">//</span> <span class="mi">2</span><span class="p">,</span> <span class="n">right</span><span class="p">)</span>
|
||||
<a id="__codelineno-22-15" name="__codelineno-22-15" href="#__codelineno-22-15"></a> <span class="c1"># 将中位数交换至数组最左端</span>
|
||||
<a id="__codelineno-22-16" name="__codelineno-22-16" href="#__codelineno-22-16"></a> <span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">],</span> <span class="n">nums</span><span class="p">[</span><span class="n">med</span><span class="p">]</span> <span class="o">=</span> <span class="n">nums</span><span class="p">[</span><span class="n">med</span><span class="p">],</span> <span class="n">nums</span><span class="p">[</span><span class="n">left</span><span class="p">]</span>
|
||||
<a id="__codelineno-22-17" name="__codelineno-22-17" href="#__codelineno-22-17"></a> <span class="c1"># 以 nums[left] 作为基准数</span>
|
||||
|
@ -2812,7 +2812,7 @@
|
|||
<a id="__codelineno-32-3" name="__codelineno-32-3" href="#__codelineno-32-3"></a> <span class="c1"># 子数组长度为 1 时终止</span>
|
||||
<a id="__codelineno-32-4" name="__codelineno-32-4" href="#__codelineno-32-4"></a> <span class="k">while</span> <span class="n">left</span> <span class="o"><</span> <span class="n">right</span><span class="p">:</span>
|
||||
<a id="__codelineno-32-5" name="__codelineno-32-5" href="#__codelineno-32-5"></a> <span class="c1"># 哨兵划分操作</span>
|
||||
<a id="__codelineno-32-6" name="__codelineno-32-6" href="#__codelineno-32-6"></a> <span class="n">pivot</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partition</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">right</span><span class="p">)</span>
|
||||
<a id="__codelineno-32-6" name="__codelineno-32-6" href="#__codelineno-32-6"></a> <span class="n">pivot</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">partition</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">right</span><span class="p">)</span>
|
||||
<a id="__codelineno-32-7" name="__codelineno-32-7" href="#__codelineno-32-7"></a> <span class="c1"># 对两个子数组中较短的那个执行快排</span>
|
||||
<a id="__codelineno-32-8" name="__codelineno-32-8" href="#__codelineno-32-8"></a> <span class="k">if</span> <span class="n">pivot</span> <span class="o">-</span> <span class="n">left</span> <span class="o"><</span> <span class="n">right</span> <span class="o">-</span> <span class="n">pivot</span><span class="p">:</span>
|
||||
<a id="__codelineno-32-9" name="__codelineno-32-9" href="#__codelineno-32-9"></a> <span class="bp">self</span><span class="o">.</span><span class="n">quick_sort</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">pivot</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># 递归排序左子数组</span>
|
||||
|
|
|
@ -2618,8 +2618,8 @@
|
|||
<a id="__codelineno-12-96" name="__codelineno-12-96" href="#__codelineno-12-96"></a>
|
||||
<a id="__codelineno-12-97" name="__codelineno-12-97" href="#__codelineno-12-97"></a> <span class="k">def</span> <span class="nf">to_array</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]:</span>
|
||||
<a id="__codelineno-12-98" name="__codelineno-12-98" href="#__codelineno-12-98"></a><span class="w"> </span><span class="sd">"""返回数组用于打印"""</span>
|
||||
<a id="__codelineno-12-99" name="__codelineno-12-99" href="#__codelineno-12-99"></a> <span class="n">node</span><span class="p">:</span> <span class="n">ListNode</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">front</span>
|
||||
<a id="__codelineno-12-100" name="__codelineno-12-100" href="#__codelineno-12-100"></a> <span class="n">res</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">()</span>
|
||||
<a id="__codelineno-12-99" name="__codelineno-12-99" href="#__codelineno-12-99"></a> <span class="n">node</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">front</span>
|
||||
<a id="__codelineno-12-100" name="__codelineno-12-100" href="#__codelineno-12-100"></a> <span class="n">res</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">()</span>
|
||||
<a id="__codelineno-12-101" name="__codelineno-12-101" href="#__codelineno-12-101"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">()):</span>
|
||||
<a id="__codelineno-12-102" name="__codelineno-12-102" href="#__codelineno-12-102"></a> <span class="n">res</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">val</span>
|
||||
<a id="__codelineno-12-103" name="__codelineno-12-103" href="#__codelineno-12-103"></a> <span class="n">node</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">next</span>
|
||||
|
|
|
@ -3118,7 +3118,7 @@
|
|||
<a id="__codelineno-22-46" name="__codelineno-22-46" href="#__codelineno-22-46"></a>
|
||||
<a id="__codelineno-22-47" name="__codelineno-22-47" href="#__codelineno-22-47"></a> <span class="k">def</span> <span class="nf">to_list</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]:</span>
|
||||
<a id="__codelineno-22-48" name="__codelineno-22-48" href="#__codelineno-22-48"></a><span class="w"> </span><span class="sd">"""返回列表用于打印"""</span>
|
||||
<a id="__codelineno-22-49" name="__codelineno-22-49" href="#__codelineno-22-49"></a> <span class="n">res</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">()</span>
|
||||
<a id="__codelineno-22-49" name="__codelineno-22-49" href="#__codelineno-22-49"></a> <span class="n">res</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">()</span>
|
||||
<a id="__codelineno-22-50" name="__codelineno-22-50" href="#__codelineno-22-50"></a> <span class="n">j</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__front</span>
|
||||
<a id="__codelineno-22-51" name="__codelineno-22-51" href="#__codelineno-22-51"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">()):</span>
|
||||
<a id="__codelineno-22-52" name="__codelineno-22-52" href="#__codelineno-22-52"></a> <span class="n">res</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__nums</span><span class="p">[(</span><span class="n">j</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">capacity</span><span class="p">())]</span>
|
||||
|
|
|
@ -2428,7 +2428,7 @@
|
|||
<a id="__codelineno-12-37" name="__codelineno-12-37" href="#__codelineno-12-37"></a>
|
||||
<a id="__codelineno-12-38" name="__codelineno-12-38" href="#__codelineno-12-38"></a> <span class="k">def</span> <span class="nf">to_list</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-></span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]:</span>
|
||||
<a id="__codelineno-12-39" name="__codelineno-12-39" href="#__codelineno-12-39"></a><span class="w"> </span><span class="sd">"""转化为列表用于打印"""</span>
|
||||
<a id="__codelineno-12-40" name="__codelineno-12-40" href="#__codelineno-12-40"></a> <span class="n">arr</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-12-40" name="__codelineno-12-40" href="#__codelineno-12-40"></a> <span class="n">arr</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-12-41" name="__codelineno-12-41" href="#__codelineno-12-41"></a> <span class="n">node</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__peek</span>
|
||||
<a id="__codelineno-12-42" name="__codelineno-12-42" href="#__codelineno-12-42"></a> <span class="k">while</span> <span class="n">node</span><span class="p">:</span>
|
||||
<a id="__codelineno-12-43" name="__codelineno-12-43" href="#__codelineno-12-43"></a> <span class="n">arr</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">val</span><span class="p">)</span>
|
||||
|
|
|
@ -2014,7 +2014,7 @@
|
|||
<a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a> <span class="n">queue</span><span class="p">:</span> <span class="n">deque</span><span class="p">[</span><span class="n">TreeNode</span><span class="p">]</span> <span class="o">=</span> <span class="n">deque</span><span class="p">()</span>
|
||||
<a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a> <span class="n">queue</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">root</span><span class="p">)</span>
|
||||
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a> <span class="c1"># 初始化一个列表,用于保存遍历序列</span>
|
||||
<a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a> <span class="n">res</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a> <span class="n">res</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a> <span class="k">while</span> <span class="n">queue</span><span class="p">:</span>
|
||||
<a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a> <span class="n">node</span><span class="p">:</span> <span class="n">TreeNode</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span> <span class="c1"># 队列出队</span>
|
||||
<a id="__codelineno-2-10" name="__codelineno-2-10" href="#__codelineno-2-10"></a> <span class="n">res</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">val</span><span class="p">)</span> <span class="c1"># 保存节点值</span>
|
||||
|
|
File diff suppressed because one or more lines are too long
120
sitemap.xml
120
sitemap.xml
|
@ -2,302 +2,302 @@
|
|||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_appendix/contribution/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_appendix/installation/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/array/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/linked_list/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/list/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/summary/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/backtracking_algorithm/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/n_queens_problem/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_backtracking/permutations_problem/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/performance_evaluation/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/space_complexity/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/summary/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/time_complexity/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/basic_data_types/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/character_encoding/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/classification_of_data_structure/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/number_encoding/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_data_structure/summary/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/graph/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/graph_operations/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/graph_traversal/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_graph/summary/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/hash_collision/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/hash_map/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_hashing/summary/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/build_heap/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/heap/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_heap/summary/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_introduction/algorithms_are_everywhere/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_introduction/summary/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_introduction/what_is_dsa/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_preface/about_the_book/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_preface/suggestions/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_preface/summary/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_reference/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/binary_search/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/binary_search_edge/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/replace_linear_by_hashing/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/searching_algorithm_revisited/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_searching/summary/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/bubble_sort/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/bucket_sort/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/counting_sort/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/insertion_sort/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/merge_sort/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/quick_sort/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/radix_sort/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/sorting_algorithm/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_sorting/summary/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/deque/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/queue/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/stack/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/summary/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/array_representation_of_tree/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/avl_tree/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/binary_search_tree/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/binary_tree/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/binary_tree_traversal/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://www.hello-algo.com/chapter_tree/summary/</loc>
|
||||
<lastmod>2023-05-21</lastmod>
|
||||
<lastmod>2023-05-22</lastmod>
|
||||
<changefreq>daily</changefreq>
|
||||
</url>
|
||||
</urlset>
|
BIN
sitemap.xml.gz
BIN
sitemap.xml.gz
Binary file not shown.
Loading…
Reference in a new issue