mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 10:56:29 +08:00
deploy
This commit is contained in:
parent
983524db0f
commit
3df839efab
14 changed files with 663 additions and 486 deletions
|
@ -3520,7 +3520,9 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">array.rs</span><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a>
|
<div class="highlight"><span class="filename">array.rs</span><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a><span class="cm">/* 初始化数组 */</span>
|
||||||
|
<a id="__codelineno-11-2" name="__codelineno-11-2" href="#__codelineno-11-2"></a><span class="kd">let</span><span class="w"> </span><span class="n">arr</span>: <span class="nb">Vec</span><span class="o"><</span><span class="kt">i32</span><span class="o">></span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">5</span><span class="p">];</span><span class="w"> </span><span class="c1">// [0, 0, 0, 0, 0]</span>
|
||||||
|
<a id="__codelineno-11-3" name="__codelineno-11-3" href="#__codelineno-11-3"></a><span class="kd">let</span><span class="w"> </span><span class="n">nums</span>: <span class="nb">Vec</span><span class="o"><</span><span class="kt">i32</span><span class="o">></span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">];</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -3596,7 +3596,14 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a>
|
<div class="highlight"><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">rc</span>::<span class="n">Rc</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-11-2" name="__codelineno-11-2" href="#__codelineno-11-2"></a><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">cell</span>::<span class="n">RefCell</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-11-3" name="__codelineno-11-3" href="#__codelineno-11-3"></a><span class="cm">/* 链表节点类 */</span>
|
||||||
|
<a id="__codelineno-11-4" name="__codelineno-11-4" href="#__codelineno-11-4"></a><span class="cp">#[derive(Debug)]</span>
|
||||||
|
<a id="__codelineno-11-5" name="__codelineno-11-5" href="#__codelineno-11-5"></a><span class="k">struct</span> <span class="nc">ListNode</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-11-6" name="__codelineno-11-6" href="#__codelineno-11-6"></a><span class="w"> </span><span class="n">val</span>: <span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="c1">// 节点值</span>
|
||||||
|
<a id="__codelineno-11-7" name="__codelineno-11-7" href="#__codelineno-11-7"></a><span class="w"> </span><span class="n">next</span>: <span class="nb">Option</span><span class="o"><</span><span class="n">Rc</span><span class="o"><</span><span class="n">RefCell</span><span class="o"><</span><span class="n">ListNode</span><span class="o">>>></span><span class="p">,</span><span class="w"> </span><span class="c1">// 指向下一节点的指针(引用)</span>
|
||||||
|
<a id="__codelineno-11-8" name="__codelineno-11-8" href="#__codelineno-11-8"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -3771,7 +3778,19 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">linked_list.rs</span><pre><span></span><code><a id="__codelineno-23-1" name="__codelineno-23-1" href="#__codelineno-23-1"></a>
|
<div class="highlight"><span class="filename">linked_list.rs</span><pre><span></span><code><a id="__codelineno-23-1" name="__codelineno-23-1" href="#__codelineno-23-1"></a><span class="cm">/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */</span>
|
||||||
|
<a id="__codelineno-23-2" name="__codelineno-23-2" href="#__codelineno-23-2"></a><span class="c1">// 初始化各个节点</span>
|
||||||
|
<a id="__codelineno-23-3" name="__codelineno-23-3" href="#__codelineno-23-3"></a><span class="kd">let</span><span class="w"> </span><span class="n">n0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Rc</span>::<span class="n">new</span><span class="p">(</span><span class="n">RefCell</span>::<span class="n">new</span><span class="p">(</span><span class="n">ListNode</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">val</span>: <span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">next</span>: <span class="nb">None</span> <span class="p">}));</span>
|
||||||
|
<a id="__codelineno-23-4" name="__codelineno-23-4" href="#__codelineno-23-4"></a><span class="kd">let</span><span class="w"> </span><span class="n">n1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Rc</span>::<span class="n">new</span><span class="p">(</span><span class="n">RefCell</span>::<span class="n">new</span><span class="p">(</span><span class="n">ListNode</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">val</span>: <span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="n">next</span>: <span class="nb">None</span> <span class="p">}));</span>
|
||||||
|
<a id="__codelineno-23-5" name="__codelineno-23-5" href="#__codelineno-23-5"></a><span class="kd">let</span><span class="w"> </span><span class="n">n2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Rc</span>::<span class="n">new</span><span class="p">(</span><span class="n">RefCell</span>::<span class="n">new</span><span class="p">(</span><span class="n">ListNode</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">val</span>: <span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="n">next</span>: <span class="nb">None</span> <span class="p">}));</span>
|
||||||
|
<a id="__codelineno-23-6" name="__codelineno-23-6" href="#__codelineno-23-6"></a><span class="kd">let</span><span class="w"> </span><span class="n">n3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Rc</span>::<span class="n">new</span><span class="p">(</span><span class="n">RefCell</span>::<span class="n">new</span><span class="p">(</span><span class="n">ListNode</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">val</span>: <span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="n">next</span>: <span class="nb">None</span> <span class="p">}));</span>
|
||||||
|
<a id="__codelineno-23-7" name="__codelineno-23-7" href="#__codelineno-23-7"></a><span class="kd">let</span><span class="w"> </span><span class="n">n4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Rc</span>::<span class="n">new</span><span class="p">(</span><span class="n">RefCell</span>::<span class="n">new</span><span class="p">(</span><span class="n">ListNode</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">val</span>: <span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">next</span>: <span class="nb">None</span> <span class="p">}));</span>
|
||||||
|
<a id="__codelineno-23-8" name="__codelineno-23-8" href="#__codelineno-23-8"></a>
|
||||||
|
<a id="__codelineno-23-9" name="__codelineno-23-9" href="#__codelineno-23-9"></a><span class="c1">// 构建引用指向</span>
|
||||||
|
<a id="__codelineno-23-10" name="__codelineno-23-10" href="#__codelineno-23-10"></a><span class="n">n0</span><span class="p">.</span><span class="n">borrow_mut</span><span class="p">().</span><span class="n">next</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">n1</span><span class="p">.</span><span class="n">clone</span><span class="p">());</span>
|
||||||
|
<a id="__codelineno-23-11" name="__codelineno-23-11" href="#__codelineno-23-11"></a><span class="n">n1</span><span class="p">.</span><span class="n">borrow_mut</span><span class="p">().</span><span class="n">next</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">n2</span><span class="p">.</span><span class="n">clone</span><span class="p">());</span>
|
||||||
|
<a id="__codelineno-23-12" name="__codelineno-23-12" href="#__codelineno-23-12"></a><span class="n">n2</span><span class="p">.</span><span class="n">borrow_mut</span><span class="p">().</span><span class="n">next</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">n3</span><span class="p">.</span><span class="n">clone</span><span class="p">());</span>
|
||||||
|
<a id="__codelineno-23-13" name="__codelineno-23-13" href="#__codelineno-23-13"></a><span class="n">n3</span><span class="p">.</span><span class="n">borrow_mut</span><span class="p">().</span><span class="n">next</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">n4</span><span class="p">.</span><span class="n">clone</span><span class="p">());</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -4536,7 +4555,27 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><pre><span></span><code><a id="__codelineno-83-1" name="__codelineno-83-1" href="#__codelineno-83-1"></a>
|
<div class="highlight"><pre><span></span><code><a id="__codelineno-83-1" name="__codelineno-83-1" href="#__codelineno-83-1"></a><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">rc</span>::<span class="n">Rc</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-83-2" name="__codelineno-83-2" href="#__codelineno-83-2"></a><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">cell</span>::<span class="n">RefCell</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-83-3" name="__codelineno-83-3" href="#__codelineno-83-3"></a>
|
||||||
|
<a id="__codelineno-83-4" name="__codelineno-83-4" href="#__codelineno-83-4"></a><span class="cm">/* 双向链表节点类型 */</span>
|
||||||
|
<a id="__codelineno-83-5" name="__codelineno-83-5" href="#__codelineno-83-5"></a><span class="cp">#[derive(Debug)]</span>
|
||||||
|
<a id="__codelineno-83-6" name="__codelineno-83-6" href="#__codelineno-83-6"></a><span class="k">struct</span> <span class="nc">ListNode</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-83-7" name="__codelineno-83-7" href="#__codelineno-83-7"></a><span class="w"> </span><span class="n">val</span>: <span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="c1">// 节点值</span>
|
||||||
|
<a id="__codelineno-83-8" name="__codelineno-83-8" href="#__codelineno-83-8"></a><span class="w"> </span><span class="n">next</span>: <span class="nb">Option</span><span class="o"><</span><span class="n">Rc</span><span class="o"><</span><span class="n">RefCell</span><span class="o"><</span><span class="n">ListNode</span><span class="o">>>></span><span class="p">,</span><span class="w"> </span><span class="c1">// 指向后继节点的指针(引用)</span>
|
||||||
|
<a id="__codelineno-83-9" name="__codelineno-83-9" href="#__codelineno-83-9"></a><span class="w"> </span><span class="n">prev</span>: <span class="nb">Option</span><span class="o"><</span><span class="n">Rc</span><span class="o"><</span><span class="n">RefCell</span><span class="o"><</span><span class="n">ListNode</span><span class="o">>>></span><span class="p">,</span><span class="w"> </span><span class="c1">// 指向前驱节点的指针(引用)</span>
|
||||||
|
<a id="__codelineno-83-10" name="__codelineno-83-10" href="#__codelineno-83-10"></a><span class="p">}</span>
|
||||||
|
<a id="__codelineno-83-11" name="__codelineno-83-11" href="#__codelineno-83-11"></a>
|
||||||
|
<a id="__codelineno-83-12" name="__codelineno-83-12" href="#__codelineno-83-12"></a><span class="cm">/* 构造函数 */</span>
|
||||||
|
<a id="__codelineno-83-13" name="__codelineno-83-13" href="#__codelineno-83-13"></a><span class="k">impl</span><span class="w"> </span><span class="n">ListNode</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-83-14" name="__codelineno-83-14" href="#__codelineno-83-14"></a><span class="w"> </span><span class="k">fn</span> <span class="nf">new</span><span class="p">(</span><span class="n">val</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span>-> <span class="nc">Self</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-83-15" name="__codelineno-83-15" href="#__codelineno-83-15"></a><span class="w"> </span><span class="n">ListNode</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-83-16" name="__codelineno-83-16" href="#__codelineno-83-16"></a><span class="w"> </span><span class="n">val</span><span class="p">,</span>
|
||||||
|
<a id="__codelineno-83-17" name="__codelineno-83-17" href="#__codelineno-83-17"></a><span class="w"> </span><span class="n">next</span>: <span class="nb">None</span><span class="p">,</span>
|
||||||
|
<a id="__codelineno-83-18" name="__codelineno-83-18" href="#__codelineno-83-18"></a><span class="w"> </span><span class="n">prev</span>: <span class="nb">None</span><span class="p">,</span>
|
||||||
|
<a id="__codelineno-83-19" name="__codelineno-83-19" href="#__codelineno-83-19"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-83-20" name="__codelineno-83-20" href="#__codelineno-83-20"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-83-21" name="__codelineno-83-21" href="#__codelineno-83-21"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -3505,7 +3505,11 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">list.rs</span><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a>
|
<div class="highlight"><span class="filename">list.rs</span><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a><span class="cm">/* 初始化列表 */</span>
|
||||||
|
<a id="__codelineno-11-2" name="__codelineno-11-2" href="#__codelineno-11-2"></a><span class="c1">// 无初始值</span>
|
||||||
|
<a id="__codelineno-11-3" name="__codelineno-11-3" href="#__codelineno-11-3"></a><span class="kd">let</span><span class="w"> </span><span class="n">list1</span>: <span class="nb">Vec</span><span class="o"><</span><span class="kt">i32</span><span class="o">></span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Vec</span>::<span class="n">new</span><span class="p">();</span>
|
||||||
|
<a id="__codelineno-11-4" name="__codelineno-11-4" href="#__codelineno-11-4"></a><span class="c1">// 有初始值</span>
|
||||||
|
<a id="__codelineno-11-5" name="__codelineno-11-5" href="#__codelineno-11-5"></a><span class="kd">let</span><span class="w"> </span><span class="n">list2</span>: <span class="nb">Vec</span><span class="o"><</span><span class="kt">i32</span><span class="o">></span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">];</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -3598,7 +3602,10 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">list.rs</span><pre><span></span><code><a id="__codelineno-23-1" name="__codelineno-23-1" href="#__codelineno-23-1"></a>
|
<div class="highlight"><span class="filename">list.rs</span><pre><span></span><code><a id="__codelineno-23-1" name="__codelineno-23-1" href="#__codelineno-23-1"></a><span class="cm">/* 访问元素 */</span>
|
||||||
|
<a id="__codelineno-23-2" name="__codelineno-23-2" href="#__codelineno-23-2"></a><span class="kd">let</span><span class="w"> </span><span class="n">num</span>: <span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="n">list</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span><span class="w"> </span><span class="c1">// 访问索引 1 处的元素</span>
|
||||||
|
<a id="__codelineno-23-3" name="__codelineno-23-3" href="#__codelineno-23-3"></a><span class="cm">/* 更新元素 */</span>
|
||||||
|
<a id="__codelineno-23-4" name="__codelineno-23-4" href="#__codelineno-23-4"></a><span class="n">list</span><span class="p">[</span><span class="mi">1</span><span class="p">]</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="c1">// 将索引 1 处的元素更新为 0</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -3791,7 +3798,21 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">list.rs</span><pre><span></span><code><a id="__codelineno-35-1" name="__codelineno-35-1" href="#__codelineno-35-1"></a>
|
<div class="highlight"><span class="filename">list.rs</span><pre><span></span><code><a id="__codelineno-35-1" name="__codelineno-35-1" href="#__codelineno-35-1"></a><span class="cm">/* 清空列表 */</span>
|
||||||
|
<a id="__codelineno-35-2" name="__codelineno-35-2" href="#__codelineno-35-2"></a><span class="n">list</span><span class="p">.</span><span class="n">clear</span><span class="p">();</span>
|
||||||
|
<a id="__codelineno-35-3" name="__codelineno-35-3" href="#__codelineno-35-3"></a>
|
||||||
|
<a id="__codelineno-35-4" name="__codelineno-35-4" href="#__codelineno-35-4"></a><span class="cm">/* 尾部添加元素 */</span>
|
||||||
|
<a id="__codelineno-35-5" name="__codelineno-35-5" href="#__codelineno-35-5"></a><span class="n">list</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-35-6" name="__codelineno-35-6" href="#__codelineno-35-6"></a><span class="n">list</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-35-7" name="__codelineno-35-7" href="#__codelineno-35-7"></a><span class="n">list</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-35-8" name="__codelineno-35-8" href="#__codelineno-35-8"></a><span class="n">list</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-35-9" name="__codelineno-35-9" href="#__codelineno-35-9"></a><span class="n">list</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="mi">4</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-35-10" name="__codelineno-35-10" href="#__codelineno-35-10"></a>
|
||||||
|
<a id="__codelineno-35-11" name="__codelineno-35-11" href="#__codelineno-35-11"></a><span class="cm">/* 中间插入元素 */</span>
|
||||||
|
<a id="__codelineno-35-12" name="__codelineno-35-12" href="#__codelineno-35-12"></a><span class="n">list</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">);</span><span class="w"> </span><span class="c1">// 在索引 3 处插入数字 6</span>
|
||||||
|
<a id="__codelineno-35-13" name="__codelineno-35-13" href="#__codelineno-35-13"></a>
|
||||||
|
<a id="__codelineno-35-14" name="__codelineno-35-14" href="#__codelineno-35-14"></a><span class="cm">/* 删除元素 */</span>
|
||||||
|
<a id="__codelineno-35-15" name="__codelineno-35-15" href="#__codelineno-35-15"></a><span class="n">list</span><span class="p">.</span><span class="n">remove</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span><span class="w"> </span><span class="c1">// 删除索引 3 处的元素</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -3943,7 +3964,17 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">list.rs</span><pre><span></span><code><a id="__codelineno-47-1" name="__codelineno-47-1" href="#__codelineno-47-1"></a>
|
<div class="highlight"><span class="filename">list.rs</span><pre><span></span><code><a id="__codelineno-47-1" name="__codelineno-47-1" href="#__codelineno-47-1"></a><span class="cm">/* 通过索引遍历列表 */</span>
|
||||||
|
<a id="__codelineno-47-2" name="__codelineno-47-2" href="#__codelineno-47-2"></a><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-47-3" name="__codelineno-47-3" href="#__codelineno-47-3"></a><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="p">)</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">list</span><span class="p">.</span><span class="n">iter</span><span class="p">().</span><span class="n">enumerate</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-47-4" name="__codelineno-47-4" href="#__codelineno-47-4"></a><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-47-5" name="__codelineno-47-5" href="#__codelineno-47-5"></a><span class="p">}</span>
|
||||||
|
<a id="__codelineno-47-6" name="__codelineno-47-6" href="#__codelineno-47-6"></a>
|
||||||
|
<a id="__codelineno-47-7" name="__codelineno-47-7" href="#__codelineno-47-7"></a><span class="cm">/* 直接遍历列表元素 */</span>
|
||||||
|
<a id="__codelineno-47-8" name="__codelineno-47-8" href="#__codelineno-47-8"></a><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-47-9" name="__codelineno-47-9" href="#__codelineno-47-9"></a><span class="k">for</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">list</span><span class="p">.</span><span class="n">iter</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-47-10" name="__codelineno-47-10" href="#__codelineno-47-10"></a><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-47-11" name="__codelineno-47-11" href="#__codelineno-47-11"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -4019,7 +4050,9 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">list.rs</span><pre><span></span><code><a id="__codelineno-59-1" name="__codelineno-59-1" href="#__codelineno-59-1"></a>
|
<div class="highlight"><span class="filename">list.rs</span><pre><span></span><code><a id="__codelineno-59-1" name="__codelineno-59-1" href="#__codelineno-59-1"></a><span class="cm">/* 拼接两个列表 */</span>
|
||||||
|
<a id="__codelineno-59-2" name="__codelineno-59-2" href="#__codelineno-59-2"></a><span class="kd">let</span><span class="w"> </span><span class="n">list1</span>: <span class="nb">Vec</span><span class="o"><</span><span class="kt">i32</span><span class="o">></span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">];</span>
|
||||||
|
<a id="__codelineno-59-3" name="__codelineno-59-3" href="#__codelineno-59-3"></a><span class="n">list</span><span class="p">.</span><span class="n">extend</span><span class="p">(</span><span class="n">list1</span><span class="p">);</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -4082,7 +4115,8 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">list.rs</span><pre><span></span><code><a id="__codelineno-71-1" name="__codelineno-71-1" href="#__codelineno-71-1"></a>
|
<div class="highlight"><span class="filename">list.rs</span><pre><span></span><code><a id="__codelineno-71-1" name="__codelineno-71-1" href="#__codelineno-71-1"></a><span class="cm">/* 排序列表 */</span>
|
||||||
|
<a id="__codelineno-71-2" name="__codelineno-71-2" href="#__codelineno-71-2"></a><span class="n">list</span><span class="p">.</span><span class="n">sort</span><span class="p">();</span><span class="w"> </span><span class="c1">// 排序后,列表元素从小到大排列</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -655,7 +655,7 @@
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
<a href="#213" class="md-nav__link">
|
<a href="#213" class="md-nav__link">
|
||||||
2.1.3. 复杂度分析重要性
|
2.1.3. 复杂度的重要性
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</li>
|
</li>
|
||||||
|
@ -3437,7 +3437,7 @@
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
<a href="#213" class="md-nav__link">
|
<a href="#213" class="md-nav__link">
|
||||||
2.1.3. 复杂度分析重要性
|
2.1.3. 复杂度的重要性
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</li>
|
</li>
|
||||||
|
@ -3467,35 +3467,36 @@
|
||||||
|
|
||||||
<h1 id="21">2.1. 算法效率评估<a class="headerlink" href="#21" title="Permanent link">¶</a></h1>
|
<h1 id="21">2.1. 算法效率评估<a class="headerlink" href="#21" title="Permanent link">¶</a></h1>
|
||||||
<h2 id="211">2.1.1. 算法评价维度<a class="headerlink" href="#211" title="Permanent link">¶</a></h2>
|
<h2 id="211">2.1.1. 算法评价维度<a class="headerlink" href="#211" title="Permanent link">¶</a></h2>
|
||||||
<p>从总体上看,算法设计追求以下两个层面的目标:</p>
|
<p>在算法设计中,我们先后追求以下两个层面的目标:</p>
|
||||||
<ol>
|
<ol>
|
||||||
<li><strong>找到问题解法</strong>。算法需要在规定的输入范围内,可靠地求得问题的正确解。</li>
|
<li><strong>找到问题解法</strong>:算法需要在规定的输入范围内,可靠地求得问题的正确解。</li>
|
||||||
<li><strong>寻求最优解法</strong>。同一个问题可能存在多种解法,我们希望找到尽可能高效的算法。</li>
|
<li><strong>寻求最优解法</strong>:同一个问题可能存在多种解法,我们希望找到尽可能高效的算法。</li>
|
||||||
</ol>
|
</ol>
|
||||||
<p>因此,在能够解决问题的前提下,算法效率成为主要的评价维度,主要包括:</p>
|
<p>因此在能够解决问题的前提下,算法效率成为主要的评价维度,包括:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li><strong>时间效率</strong>,即算法运行速度的快慢。</li>
|
<li><strong>时间效率</strong>,即算法运行速度的快慢。</li>
|
||||||
<li><strong>空间效率</strong>,即算法占用内存空间的大小。</li>
|
<li><strong>空间效率</strong>,即算法占用内存空间的大小。</li>
|
||||||
</ul>
|
</ul>
|
||||||
<p>简而言之,<strong>我们的目标是设计“既快又省”的数据结构与算法</strong>。掌握评估算法效率的方法则至关重要,因为只有了解评价标准,我们才能进行算法之间的对比分析,从而指导算法设计与优化过程。</p>
|
<p>简而言之,<strong>我们的目标是设计“既快又省”的数据结构与算法</strong>。而有效地评估算法效率至关重要,因为只有了解评价标准,我们才能对比分析各种算法,从而指导算法设计与优化过程。</p>
|
||||||
<h2 id="212">2.1.2. 效率评估方法<a class="headerlink" href="#212" title="Permanent link">¶</a></h2>
|
<h2 id="212">2.1.2. 效率评估方法<a class="headerlink" href="#212" title="Permanent link">¶</a></h2>
|
||||||
<h3 id="_1">实际测试<a class="headerlink" href="#_1" title="Permanent link">¶</a></h3>
|
<h3 id="_1">实际测试<a class="headerlink" href="#_1" title="Permanent link">¶</a></h3>
|
||||||
<p>假设我们现在有算法 A 和算法 B,它们都能解决同一问题,现在需要对比这两个算法的效率。我们最直接的方法就是找一台计算机,运行这两个算法,并监控记录它们的运行时间和内存占用情况。这种评估方式能够反映真实情况,但也存在较大局限性。</p>
|
<p>假设我们现在有算法 <code>A</code> 和算法 <code>B</code> ,它们都能解决同一问题,现在需要对比这两个算法的效率。最直接的方法是找一台计算机,运行这两个算法,并监控记录它们的运行时间和内存占用情况。这种评估方式能够反映真实情况,但也存在较大局限性。</p>
|
||||||
<p><strong>难以排除测试环境的干扰因素</strong>。硬件配置会影响算法的性能表现。例如,在某台计算机中,算法 A 的运行时间比算法 B 短;但在另一台配置不同的计算机中,我们可能得到相反的测试结果。这意味着我们需要在各种机器上进行测试,而这是不现实的。</p>
|
<p><strong>难以排除测试环境的干扰因素</strong>。硬件配置会影响算法的性能表现。比如在某台计算机中,算法 <code>A</code> 的运行时间比算法 <code>B</code> 短;但在另一台配置不同的计算机中,我们可能得到相反的测试结果。这意味着我们需要在各种机器上进行测试,统计平均效率,而这是不现实的。</p>
|
||||||
<p><strong>展开完整测试非常耗费资源</strong>。随着输入数据量的变化,算法会表现出不同的效率。例如,输入数据量较小时,算法 A 的运行时间可能短于算法 B;而输入数据量较大时,测试结果可能相反。因此,为了得到有说服力的结论,我们需要测试各种规模的输入数据,这样需要占用大量的计算资源。</p>
|
<p><strong>展开完整测试非常耗费资源</strong>。随着输入数据量的变化,算法会表现出不同的效率。例如,在输入数据量较小时,算法 <code>A</code> 的运行时间比算法 <code>B</code> 更少;而输入数据量较大时,测试结果可能恰恰相反。因此,为了得到有说服力的结论,我们需要测试各种规模的输入数据,而这样需要耗费大量的计算资源。</p>
|
||||||
<h3 id="_2">理论估算<a class="headerlink" href="#_2" title="Permanent link">¶</a></h3>
|
<h3 id="_2">理论估算<a class="headerlink" href="#_2" title="Permanent link">¶</a></h3>
|
||||||
<p>由于实际测试具有较大的局限性,我们可以考虑仅通过一些计算来评估算法的效率。这种估算方法被称为「复杂度分析 Complexity Analysis」或「渐近复杂度分析 Asymptotic Complexity Analysis」。</p>
|
<p>由于实际测试具有较大的局限性,我们可以考虑仅通过一些计算来评估算法的效率。这种估算方法被称为「渐近复杂度分析 Asymptotic Complexity Analysis」,简称为「复杂度分析」。</p>
|
||||||
<p><strong>复杂度分析评估的是算法运行效率随着输入数据量增多时的增长趋势</strong>。这个定义有些拗口,我们可以将其分为三个重点来理解:</p>
|
<p><strong>复杂度分析评估的是算法运行效率随着输入数据量增多时的增长趋势</strong>。这个定义有些拗口,我们可以将其分为三个重点来理解:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li>“算法运行效率”可分为“运行时间”和“占用空间”,因此我们可以将复杂度分为「时间复杂度 Time Complexity」和「空间复杂度 Space Complexity」。</li>
|
<li>“算法运行效率”可分为运行时间和占用空间两部分,与之对应地,复杂度可分为「时间复杂度 Time Complexity」和「空间复杂度 Space Complexity」。</li>
|
||||||
<li>“随着输入数据量增多时”表示复杂度与输入数据量有关,反映了算法运行效率与输入数据量之间的关系。</li>
|
<li>“随着输入数据量增多时”表示复杂度与输入数据量有关,反映了算法运行效率与输入数据量之间的关系。</li>
|
||||||
<li>“增长趋势”表示复杂度分析关注的是算法时间与空间的增长趋势,而非具体的运行时间或占用空间。</li>
|
<li>“增长趋势”表示复杂度分析关注的是算法时间与空间的增长趋势,而非具体的运行时间或占用空间。</li>
|
||||||
</ul>
|
</ul>
|
||||||
<p><strong>复杂度分析克服了实际测试方法的弊端</strong>。首先,它独立于测试环境,因此分析结果适用于所有运行平台。其次,它可以体现不同数据量下的算法效率,尤其是在大数据量下的算法性能。</p>
|
<p><strong>复杂度分析克服了实际测试方法的弊端</strong>。首先,它独立于测试环境,因此分析结果适用于所有运行平台。其次,它可以体现不同数据量下的算法效率,尤其是在大数据量下的算法性能。</p>
|
||||||
<p>如果你对复杂度分析的概念仍感到困惑,无需担心,我们会在后续章节详细介绍。</p>
|
<p>如果你对复杂度分析的概念仍感到困惑,无需担心,我们会在后续章节详细介绍。</p>
|
||||||
<h2 id="213">2.1.3. 复杂度分析重要性<a class="headerlink" href="#213" title="Permanent link">¶</a></h2>
|
<h2 id="213">2.1.3. 复杂度的重要性<a class="headerlink" href="#213" title="Permanent link">¶</a></h2>
|
||||||
<p>复杂度分析为我们提供了一把评估算法效率的“标尺”,告诉我们执行某个算法所需的时间和空间资源,并使我们能够对比不同算法之间的效率。</p>
|
<p>复杂度分析为我们提供了一把评估算法效率的“标尺”,帮助我们衡量了执行某个算法所需的时间和空间资源,并使我们能够对比不同算法之间的效率。</p>
|
||||||
<p>复杂度是个数学概念,对于初学者可能比较抽象,学习难度相对较高。从这个角度看,复杂度分析可能不太适合作为第一章的内容。然而,当我们讨论某个数据结构或算法的特点时,我们难以避免要分析其运行速度和空间使用情况。<strong>因此,在深入学习数据结构与算法之前,建议读者先对复杂度建立初步的了解,并能够完成简单案例的复杂度分析</strong>。</p>
|
<p>复杂度是个数学概念,对于初学者可能比较抽象,学习难度相对较高。从这个角度看,复杂度分析可能不太适合作为第一章的内容。</p>
|
||||||
|
<p>然而,当我们讨论某个数据结构或算法的特点时,难以避免要分析其运行速度和空间使用情况。因此,在深入学习数据结构与算法之前,<strong>建议你先对复杂度建立初步的了解,能够完成简单算法的复杂度分析</strong>。</p>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3522,22 +3522,22 @@
|
||||||
|
|
||||||
|
|
||||||
<h1 id="23">2.3. 空间复杂度<a class="headerlink" href="#23" title="Permanent link">¶</a></h1>
|
<h1 id="23">2.3. 空间复杂度<a class="headerlink" href="#23" title="Permanent link">¶</a></h1>
|
||||||
<p>「空间复杂度 Space Complexity」用于衡量算法使用内存空间随着数据量变大时的增长趋势。这个概念与时间复杂度非常类似。</p>
|
<p>「空间复杂度 Space Complexity」用于衡量算法占用内存空间随着数据量变大时的增长趋势。这个概念与时间复杂度非常类似,只需将“运行时间”替换为“占用内存空间”。</p>
|
||||||
<h2 id="231">2.3.1. 算法相关空间<a class="headerlink" href="#231" title="Permanent link">¶</a></h2>
|
<h2 id="231">2.3.1. 算法相关空间<a class="headerlink" href="#231" title="Permanent link">¶</a></h2>
|
||||||
<p>算法运行过程中使用的内存空间主要包括以下几种:</p>
|
<p>算法运行过程中使用的内存空间主要包括以下几种:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li>「输入空间」用于存储算法的输入数据。</li>
|
<li><strong>输入空间</strong>:用于存储算法的输入数据。</li>
|
||||||
<li>「暂存空间」用于存储算法运行过程中的变量、对象、函数上下文等数据。</li>
|
<li><strong>暂存空间</strong>:用于存储算法运行过程中的变量、对象、函数上下文等数据。</li>
|
||||||
<li>「输出空间」用于存储算法的输出数据。</li>
|
<li><strong>输出空间</strong>:用于存储算法的输出数据。</li>
|
||||||
</ul>
|
</ul>
|
||||||
<p>通常情况下,空间复杂度统计范围是「暂存空间」+「输出空间」。</p>
|
<p>一般情况下,空间复杂度的统计范围是“暂存空间”加上“输出空间”。</p>
|
||||||
<p>暂存空间可以进一步划分为三个部分:</p>
|
<p>暂存空间可以进一步划分为三个部分:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li>「暂存数据」用于保存算法运行过程中的各种常量、变量、对象等。</li>
|
<li><strong>暂存数据</strong>:用于保存算法运行过程中的各种常量、变量、对象等。</li>
|
||||||
<li>「栈帧空间」用于保存调用函数的上下文数据。系统在每次调用函数时都会在栈顶部创建一个栈帧,函数返回后,栈帧空间会被释放。</li>
|
<li><strong>栈帧空间</strong>:用于保存调用函数的上下文数据。系统在每次调用函数时都会在栈顶部创建一个栈帧,函数返回后,栈帧空间会被释放。</li>
|
||||||
<li>「指令空间」用于保存编译后的程序指令,在实际统计中通常忽略不计。</li>
|
<li><strong>指令空间</strong>:用于保存编译后的程序指令,在实际统计中通常忽略不计。</li>
|
||||||
</ul>
|
</ul>
|
||||||
<p>因此,在分析一段程序的空间复杂度时,我们一般统计 <strong>暂存数据、输出数据、栈帧空间</strong> 三部分。</p>
|
<p>因此在分析一段程序的空间复杂度时,<strong>我们通常统计暂存数据、输出数据、栈帧空间三部分</strong>。</p>
|
||||||
<p><img alt="算法使用的相关空间" src="../space_complexity.assets/space_types.png" /></p>
|
<p><img alt="算法使用的相关空间" src="../space_complexity.assets/space_types.png" /></p>
|
||||||
<p align="center"> Fig. 算法使用的相关空间 </p>
|
<p align="center"> Fig. 算法使用的相关空间 </p>
|
||||||
|
|
||||||
|
@ -3786,12 +3786,13 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<h2 id="232">2.3.2. 推算方法<a class="headerlink" href="#232" title="Permanent link">¶</a></h2>
|
<h2 id="232">2.3.2. 推算方法<a class="headerlink" href="#232" title="Permanent link">¶</a></h2>
|
||||||
<p>空间复杂度的推算方法与时间复杂度大致相同,只是将统计对象从“计算操作数量”转为“使用空间大小”。与时间复杂度不同的是,<strong>我们通常只关注「最差空间复杂度」</strong>,这是因为内存空间是一项硬性要求,我们必须确保在所有输入数据下都有足够的内存空间预留。</p>
|
<p>空间复杂度的推算方法与时间复杂度大致相同,只需将统计对象从“计算操作数量”转为“使用空间大小”。</p>
|
||||||
<p><strong>最差空间复杂度中的“最差”有两层含义</strong>,分别是输入数据的最差分布和算法运行过程中的最差时间点。</p>
|
<p>而与时间复杂度不同的是,<strong>我们通常只关注「最差空间复杂度」</strong>。这是因为内存空间是一项硬性要求,我们必须确保在所有输入数据下都有足够的内存空间预留。</p>
|
||||||
<ul>
|
<p>观察以下代码,最差空间复杂度中的“最差”有两层含义。</p>
|
||||||
<li><strong>以最差输入数据为准</strong>。当 <span class="arithmatex">\(n < 10\)</span> 时,空间复杂度为 <span class="arithmatex">\(O(1)\)</span> ;但当 <span class="arithmatex">\(n > 10\)</span> 时,初始化的数组 <code>nums</code> 占用 <span class="arithmatex">\(O(n)\)</span> 空间;因此最差空间复杂度为 <span class="arithmatex">\(O(n)\)</span> 。</li>
|
<ol>
|
||||||
<li><strong>以算法运行过程中的峰值内存为准</strong>。例如,程序在执行最后一行之前,占用 <span class="arithmatex">\(O(1)\)</span> 空间;当初始化数组 <code>nums</code> 时,程序占用 <span class="arithmatex">\(O(n)\)</span> 空间;因此最差空间复杂度为 <span class="arithmatex">\(O(n)\)</span> 。</li>
|
<li><strong>以最差输入数据为准</strong>:当 <span class="arithmatex">\(n < 10\)</span> 时,空间复杂度为 <span class="arithmatex">\(O(1)\)</span> ;但当 <span class="arithmatex">\(n > 10\)</span> 时,初始化的数组 <code>nums</code> 占用 <span class="arithmatex">\(O(n)\)</span> 空间;因此最差空间复杂度为 <span class="arithmatex">\(O(n)\)</span> 。</li>
|
||||||
</ul>
|
<li><strong>以算法运行中的峰值内存为准</strong>:例如,程序在执行最后一行之前,占用 <span class="arithmatex">\(O(1)\)</span> 空间;当初始化数组 <code>nums</code> 时,程序占用 <span class="arithmatex">\(O(n)\)</span> 空间;因此最差空间复杂度为 <span class="arithmatex">\(O(n)\)</span> 。</li>
|
||||||
|
</ol>
|
||||||
<div class="tabbed-set tabbed-alternate" data-tabs="2:12"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><input id="__tabbed_2_12" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Java</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Python</label><label for="__tabbed_2_4">Go</label><label for="__tabbed_2_5">JS</label><label for="__tabbed_2_6">TS</label><label for="__tabbed_2_7">C</label><label for="__tabbed_2_8">C#</label><label for="__tabbed_2_9">Swift</label><label for="__tabbed_2_10">Zig</label><label for="__tabbed_2_11">Dart</label><label for="__tabbed_2_12">Rust</label></div>
|
<div class="tabbed-set tabbed-alternate" data-tabs="2:12"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><input id="__tabbed_2_12" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Java</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Python</label><label for="__tabbed_2_4">Go</label><label for="__tabbed_2_5">JS</label><label for="__tabbed_2_6">TS</label><label for="__tabbed_2_7">C</label><label for="__tabbed_2_8">C#</label><label for="__tabbed_2_9">Swift</label><label for="__tabbed_2_10">Zig</label><label for="__tabbed_2_11">Dart</label><label for="__tabbed_2_12">Rust</label></div>
|
||||||
<div class="tabbed-content">
|
<div class="tabbed-content">
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -3901,7 +3902,11 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p><strong>在递归函数中,需要注意统计栈帧空间</strong>。例如,函数 <code>loop()</code> 在循环中调用了 <span class="arithmatex">\(n\)</span> 次 <code>function()</code> ,每轮中的 <code>function()</code> 都返回并释放了栈帧空间,因此空间复杂度仍为 <span class="arithmatex">\(O(1)\)</span> 。而递归函数 <code>recur()</code> 在运行过程中会同时存在 <span class="arithmatex">\(n\)</span> 个未返回的 <code>recur()</code> ,从而占用 <span class="arithmatex">\(O(n)\)</span> 的栈帧空间。</p>
|
<p><strong>在递归函数中,需要注意统计栈帧空间</strong>。例如以下代码:</p>
|
||||||
|
<ul>
|
||||||
|
<li>函数 <code>loop()</code> 在循环中调用了 <span class="arithmatex">\(n\)</span> 次 <code>function()</code> ,每轮中的 <code>function()</code> 都返回并释放了栈帧空间,因此空间复杂度仍为 <span class="arithmatex">\(O(1)\)</span> 。</li>
|
||||||
|
<li>递归函数 <code>recur()</code> 在运行过程中会同时存在 <span class="arithmatex">\(n\)</span> 个未返回的 <code>recur()</code> ,从而占用 <span class="arithmatex">\(O(n)\)</span> 的栈帧空间。</li>
|
||||||
|
</ul>
|
||||||
<div class="tabbed-set tabbed-alternate" data-tabs="3:12"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><input id="__tabbed_3_3" name="__tabbed_3" type="radio" /><input id="__tabbed_3_4" name="__tabbed_3" type="radio" /><input id="__tabbed_3_5" name="__tabbed_3" type="radio" /><input id="__tabbed_3_6" name="__tabbed_3" type="radio" /><input id="__tabbed_3_7" name="__tabbed_3" type="radio" /><input id="__tabbed_3_8" name="__tabbed_3" type="radio" /><input id="__tabbed_3_9" name="__tabbed_3" type="radio" /><input id="__tabbed_3_10" name="__tabbed_3" type="radio" /><input id="__tabbed_3_11" name="__tabbed_3" type="radio" /><input id="__tabbed_3_12" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1">Java</label><label for="__tabbed_3_2">C++</label><label for="__tabbed_3_3">Python</label><label for="__tabbed_3_4">Go</label><label for="__tabbed_3_5">JS</label><label for="__tabbed_3_6">TS</label><label for="__tabbed_3_7">C</label><label for="__tabbed_3_8">C#</label><label for="__tabbed_3_9">Swift</label><label for="__tabbed_3_10">Zig</label><label for="__tabbed_3_11">Dart</label><label for="__tabbed_3_12">Rust</label></div>
|
<div class="tabbed-set tabbed-alternate" data-tabs="3:12"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><input id="__tabbed_3_3" name="__tabbed_3" type="radio" /><input id="__tabbed_3_4" name="__tabbed_3" type="radio" /><input id="__tabbed_3_5" name="__tabbed_3" type="radio" /><input id="__tabbed_3_6" name="__tabbed_3" type="radio" /><input id="__tabbed_3_7" name="__tabbed_3" type="radio" /><input id="__tabbed_3_8" name="__tabbed_3" type="radio" /><input id="__tabbed_3_9" name="__tabbed_3" type="radio" /><input id="__tabbed_3_10" name="__tabbed_3" type="radio" /><input id="__tabbed_3_11" name="__tabbed_3" type="radio" /><input id="__tabbed_3_12" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1">Java</label><label for="__tabbed_3_2">C++</label><label for="__tabbed_3_3">Python</label><label for="__tabbed_3_4">Go</label><label for="__tabbed_3_5">JS</label><label for="__tabbed_3_6">TS</label><label for="__tabbed_3_7">C</label><label for="__tabbed_3_8">C#</label><label for="__tabbed_3_9">Swift</label><label for="__tabbed_3_10">Zig</label><label for="__tabbed_3_11">Dart</label><label for="__tabbed_3_12">Rust</label></div>
|
||||||
<div class="tabbed-content">
|
<div class="tabbed-content">
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -4102,7 +4107,7 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<h2 id="233">2.3.3. 常见类型<a class="headerlink" href="#233" title="Permanent link">¶</a></h2>
|
<h2 id="233">2.3.3. 常见类型<a class="headerlink" href="#233" title="Permanent link">¶</a></h2>
|
||||||
<p>设输入数据大小为 <span class="arithmatex">\(n\)</span> ,常见的空间复杂度类型有(从低到高排列)</p>
|
<p>设输入数据大小为 <span class="arithmatex">\(n\)</span> ,常见的空间复杂度类型有(从低到高排列):</p>
|
||||||
<div class="arithmatex">\[
|
<div class="arithmatex">\[
|
||||||
\begin{aligned}
|
\begin{aligned}
|
||||||
O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
||||||
|
@ -4114,7 +4119,7 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
||||||
|
|
||||||
<div class="admonition tip">
|
<div class="admonition tip">
|
||||||
<p class="admonition-title">Tip</p>
|
<p class="admonition-title">Tip</p>
|
||||||
<p>部分示例代码需要一些前置知识,包括数组、链表、二叉树、递归算法等。如果遇到看不懂的地方无需担心,可以在学习完后面章节后再来复习,现阶段我们先专注于理解空间复杂度的含义和推算方法。</p>
|
<p>部分示例代码需要一些前置知识,包括数组、链表、二叉树、递归算法等。如果你遇到看不懂的地方,可以在学习完后面章节后再来复习。</p>
|
||||||
</div>
|
</div>
|
||||||
<h3 id="o1">常数阶 <span class="arithmatex">\(O(1)\)</span><a class="headerlink" href="#o1" title="Permanent link">¶</a></h3>
|
<h3 id="o1">常数阶 <span class="arithmatex">\(O(1)\)</span><a class="headerlink" href="#o1" title="Permanent link">¶</a></h3>
|
||||||
<p>常数阶常见于数量与输入数据大小 <span class="arithmatex">\(n\)</span> 无关的常量、变量、对象。</p>
|
<p>常数阶常见于数量与输入数据大小 <span class="arithmatex">\(n\)</span> 无关的常量、变量、对象。</p>
|
||||||
|
@ -4122,238 +4127,306 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
||||||
<div class="tabbed-set tabbed-alternate" data-tabs="4:12"><input checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio" /><input id="__tabbed_4_2" name="__tabbed_4" type="radio" /><input id="__tabbed_4_3" name="__tabbed_4" type="radio" /><input id="__tabbed_4_4" name="__tabbed_4" type="radio" /><input id="__tabbed_4_5" name="__tabbed_4" type="radio" /><input id="__tabbed_4_6" name="__tabbed_4" type="radio" /><input id="__tabbed_4_7" name="__tabbed_4" type="radio" /><input id="__tabbed_4_8" name="__tabbed_4" type="radio" /><input id="__tabbed_4_9" name="__tabbed_4" type="radio" /><input id="__tabbed_4_10" name="__tabbed_4" type="radio" /><input id="__tabbed_4_11" name="__tabbed_4" type="radio" /><input id="__tabbed_4_12" name="__tabbed_4" type="radio" /><div class="tabbed-labels"><label for="__tabbed_4_1">Java</label><label for="__tabbed_4_2">C++</label><label for="__tabbed_4_3">Python</label><label for="__tabbed_4_4">Go</label><label for="__tabbed_4_5">JS</label><label for="__tabbed_4_6">TS</label><label for="__tabbed_4_7">C</label><label for="__tabbed_4_8">C#</label><label for="__tabbed_4_9">Swift</label><label for="__tabbed_4_10">Zig</label><label for="__tabbed_4_11">Dart</label><label for="__tabbed_4_12">Rust</label></div>
|
<div class="tabbed-set tabbed-alternate" data-tabs="4:12"><input checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio" /><input id="__tabbed_4_2" name="__tabbed_4" type="radio" /><input id="__tabbed_4_3" name="__tabbed_4" type="radio" /><input id="__tabbed_4_4" name="__tabbed_4" type="radio" /><input id="__tabbed_4_5" name="__tabbed_4" type="radio" /><input id="__tabbed_4_6" name="__tabbed_4" type="radio" /><input id="__tabbed_4_7" name="__tabbed_4" type="radio" /><input id="__tabbed_4_8" name="__tabbed_4" type="radio" /><input id="__tabbed_4_9" name="__tabbed_4" type="radio" /><input id="__tabbed_4_10" name="__tabbed_4" type="radio" /><input id="__tabbed_4_11" name="__tabbed_4" type="radio" /><input id="__tabbed_4_12" name="__tabbed_4" type="radio" /><div class="tabbed-labels"><label for="__tabbed_4_1">Java</label><label for="__tabbed_4_2">C++</label><label for="__tabbed_4_3">Python</label><label for="__tabbed_4_4">Go</label><label for="__tabbed_4_5">JS</label><label for="__tabbed_4_6">TS</label><label for="__tabbed_4_7">C</label><label for="__tabbed_4_8">C#</label><label for="__tabbed_4_9">Swift</label><label for="__tabbed_4_10">Zig</label><label for="__tabbed_4_11">Dart</label><label for="__tabbed_4_12">Rust</label></div>
|
||||||
<div class="tabbed-content">
|
<div class="tabbed-content">
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">space_complexity.java</span><pre><span></span><code><a id="__codelineno-36-1" name="__codelineno-36-1" href="#__codelineno-36-1"></a><span class="cm">/* 常数阶 */</span>
|
<div class="highlight"><span class="filename">space_complexity.java</span><pre><span></span><code><a id="__codelineno-36-1" name="__codelineno-36-1" href="#__codelineno-36-1"></a><span class="cm">/* 函数 */</span>
|
||||||
<a id="__codelineno-36-2" name="__codelineno-36-2" href="#__codelineno-36-2"></a><span class="kt">void</span><span class="w"> </span><span class="nf">constant</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-36-2" name="__codelineno-36-2" href="#__codelineno-36-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">function</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-36-3" name="__codelineno-36-3" href="#__codelineno-36-3"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
<a id="__codelineno-36-3" name="__codelineno-36-3" href="#__codelineno-36-3"></a><span class="w"> </span><span class="c1">// do something</span>
|
||||||
<a id="__codelineno-36-4" name="__codelineno-36-4" href="#__codelineno-36-4"></a><span class="w"> </span><span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-36-4" name="__codelineno-36-4" href="#__codelineno-36-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-36-5" name="__codelineno-36-5" href="#__codelineno-36-5"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-36-5" name="__codelineno-36-5" href="#__codelineno-36-5"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-36-6" name="__codelineno-36-6" href="#__codelineno-36-6"></a><span class="w"> </span><span class="kt">int</span><span class="o">[]</span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">int</span><span class="o">[</span><span class="mi">10000</span><span class="o">]</span><span class="p">;</span>
|
<a id="__codelineno-36-6" name="__codelineno-36-6" href="#__codelineno-36-6"></a>
|
||||||
<a id="__codelineno-36-7" name="__codelineno-36-7" href="#__codelineno-36-7"></a><span class="w"> </span><span class="n">ListNode</span><span class="w"> </span><span class="n">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ListNode</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
<a id="__codelineno-36-7" name="__codelineno-36-7" href="#__codelineno-36-7"></a><span class="cm">/* 常数阶 */</span>
|
||||||
<a id="__codelineno-36-8" name="__codelineno-36-8" href="#__codelineno-36-8"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
<a id="__codelineno-36-8" name="__codelineno-36-8" href="#__codelineno-36-8"></a><span class="kt">void</span><span class="w"> </span><span class="nf">constant</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-36-9" name="__codelineno-36-9" href="#__codelineno-36-9"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-36-9" name="__codelineno-36-9" href="#__codelineno-36-9"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-36-10" name="__codelineno-36-10" href="#__codelineno-36-10"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-36-10" name="__codelineno-36-10" href="#__codelineno-36-10"></a><span class="w"> </span><span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-36-11" name="__codelineno-36-11" href="#__codelineno-36-11"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-36-11" name="__codelineno-36-11" href="#__codelineno-36-11"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-36-12" name="__codelineno-36-12" href="#__codelineno-36-12"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
<a id="__codelineno-36-12" name="__codelineno-36-12" href="#__codelineno-36-12"></a><span class="w"> </span><span class="kt">int</span><span class="o">[]</span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">int</span><span class="o">[</span><span class="mi">10000</span><span class="o">]</span><span class="p">;</span>
|
||||||
<a id="__codelineno-36-13" name="__codelineno-36-13" href="#__codelineno-36-13"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-36-13" name="__codelineno-36-13" href="#__codelineno-36-13"></a><span class="w"> </span><span class="n">ListNode</span><span class="w"> </span><span class="n">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ListNode</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
||||||
<a id="__codelineno-36-14" name="__codelineno-36-14" href="#__codelineno-36-14"></a><span class="w"> </span><span class="n">function</span><span class="p">();</span>
|
<a id="__codelineno-36-14" name="__codelineno-36-14" href="#__codelineno-36-14"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-36-15" name="__codelineno-36-15" href="#__codelineno-36-15"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-36-15" name="__codelineno-36-15" href="#__codelineno-36-15"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-36-16" name="__codelineno-36-16" href="#__codelineno-36-16"></a><span class="p">}</span>
|
<a id="__codelineno-36-16" name="__codelineno-36-16" href="#__codelineno-36-16"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-36-17" name="__codelineno-36-17" href="#__codelineno-36-17"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-36-18" name="__codelineno-36-18" href="#__codelineno-36-18"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
||||||
|
<a id="__codelineno-36-19" name="__codelineno-36-19" href="#__codelineno-36-19"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-36-20" name="__codelineno-36-20" href="#__codelineno-36-20"></a><span class="w"> </span><span class="n">function</span><span class="p">();</span>
|
||||||
|
<a id="__codelineno-36-21" name="__codelineno-36-21" href="#__codelineno-36-21"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-36-22" name="__codelineno-36-22" href="#__codelineno-36-22"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">space_complexity.cpp</span><pre><span></span><code><a id="__codelineno-37-1" name="__codelineno-37-1" href="#__codelineno-37-1"></a><span class="cm">/* 常数阶 */</span>
|
<div class="highlight"><span class="filename">space_complexity.cpp</span><pre><span></span><code><a id="__codelineno-37-1" name="__codelineno-37-1" href="#__codelineno-37-1"></a><span class="cm">/* 函数 */</span>
|
||||||
<a id="__codelineno-37-2" name="__codelineno-37-2" href="#__codelineno-37-2"></a><span class="kt">void</span><span class="w"> </span><span class="nf">constant</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-2" name="__codelineno-37-2" href="#__codelineno-37-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">func</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-3" name="__codelineno-37-3" href="#__codelineno-37-3"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
<a id="__codelineno-37-3" name="__codelineno-37-3" href="#__codelineno-37-3"></a><span class="w"> </span><span class="c1">// do something</span>
|
||||||
<a id="__codelineno-37-4" name="__codelineno-37-4" href="#__codelineno-37-4"></a><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-37-4" name="__codelineno-37-4" href="#__codelineno-37-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-5" name="__codelineno-37-5" href="#__codelineno-37-5"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-37-5" name="__codelineno-37-5" href="#__codelineno-37-5"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-37-6" name="__codelineno-37-6" href="#__codelineno-37-6"></a><span class="w"> </span><span class="n">vector</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="w"> </span><span class="n">nums</span><span class="p">(</span><span class="mi">10000</span><span class="p">);</span>
|
<a id="__codelineno-37-6" name="__codelineno-37-6" href="#__codelineno-37-6"></a>
|
||||||
<a id="__codelineno-37-7" name="__codelineno-37-7" href="#__codelineno-37-7"></a><span class="w"> </span><span class="n">ListNode</span><span class="w"> </span><span class="n">node</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
<a id="__codelineno-37-7" name="__codelineno-37-7" href="#__codelineno-37-7"></a><span class="cm">/* 常数阶 */</span>
|
||||||
<a id="__codelineno-37-8" name="__codelineno-37-8" href="#__codelineno-37-8"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
<a id="__codelineno-37-8" name="__codelineno-37-8" href="#__codelineno-37-8"></a><span class="kt">void</span><span class="w"> </span><span class="nf">constant</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-9" name="__codelineno-37-9" href="#__codelineno-37-9"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-9" name="__codelineno-37-9" href="#__codelineno-37-9"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-37-10" name="__codelineno-37-10" href="#__codelineno-37-10"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-37-10" name="__codelineno-37-10" href="#__codelineno-37-10"></a><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-11" name="__codelineno-37-11" href="#__codelineno-37-11"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-11" name="__codelineno-37-11" href="#__codelineno-37-11"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-37-12" name="__codelineno-37-12" href="#__codelineno-37-12"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
<a id="__codelineno-37-12" name="__codelineno-37-12" href="#__codelineno-37-12"></a><span class="w"> </span><span class="n">vector</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="w"> </span><span class="n">nums</span><span class="p">(</span><span class="mi">10000</span><span class="p">);</span>
|
||||||
<a id="__codelineno-37-13" name="__codelineno-37-13" href="#__codelineno-37-13"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-37-13" name="__codelineno-37-13" href="#__codelineno-37-13"></a><span class="w"> </span><span class="n">ListNode</span><span class="w"> </span><span class="n">node</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
||||||
<a id="__codelineno-37-14" name="__codelineno-37-14" href="#__codelineno-37-14"></a><span class="w"> </span><span class="n">func</span><span class="p">();</span>
|
<a id="__codelineno-37-14" name="__codelineno-37-14" href="#__codelineno-37-14"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-37-15" name="__codelineno-37-15" href="#__codelineno-37-15"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-37-15" name="__codelineno-37-15" href="#__codelineno-37-15"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-37-16" name="__codelineno-37-16" href="#__codelineno-37-16"></a><span class="p">}</span>
|
<a id="__codelineno-37-16" name="__codelineno-37-16" href="#__codelineno-37-16"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-37-17" name="__codelineno-37-17" href="#__codelineno-37-17"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-37-18" name="__codelineno-37-18" href="#__codelineno-37-18"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
||||||
|
<a id="__codelineno-37-19" name="__codelineno-37-19" href="#__codelineno-37-19"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-37-20" name="__codelineno-37-20" href="#__codelineno-37-20"></a><span class="w"> </span><span class="n">func</span><span class="p">();</span>
|
||||||
|
<a id="__codelineno-37-21" name="__codelineno-37-21" href="#__codelineno-37-21"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-37-22" name="__codelineno-37-22" href="#__codelineno-37-22"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">space_complexity.py</span><pre><span></span><code><a id="__codelineno-38-1" name="__codelineno-38-1" href="#__codelineno-38-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>
|
<div class="highlight"><span class="filename">space_complexity.py</span><pre><span></span><code><a id="__codelineno-38-1" name="__codelineno-38-1" href="#__codelineno-38-1"></a><span class="k">def</span> <span class="nf">function</span><span class="p">()</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||||
<a id="__codelineno-38-2" name="__codelineno-38-2" href="#__codelineno-38-2"></a><span class="w"> </span><span class="sd">"""常数阶"""</span>
|
<a id="__codelineno-38-2" name="__codelineno-38-2" href="#__codelineno-38-2"></a><span class="w"> </span><span class="sd">"""函数"""</span>
|
||||||
<a id="__codelineno-38-3" name="__codelineno-38-3" href="#__codelineno-38-3"></a> <span class="c1"># 常量、变量、对象占用 O(1) 空间</span>
|
<a id="__codelineno-38-3" name="__codelineno-38-3" href="#__codelineno-38-3"></a> <span class="c1"># do something</span>
|
||||||
<a id="__codelineno-38-4" name="__codelineno-38-4" href="#__codelineno-38-4"></a> <span class="n">a</span> <span class="o">=</span> <span class="mi">0</span>
|
<a id="__codelineno-38-4" name="__codelineno-38-4" href="#__codelineno-38-4"></a> <span class="k">return</span> <span class="mi">0</span>
|
||||||
<a id="__codelineno-38-5" name="__codelineno-38-5" href="#__codelineno-38-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-38-5" name="__codelineno-38-5" href="#__codelineno-38-5"></a>
|
||||||
<a id="__codelineno-38-6" name="__codelineno-38-6" href="#__codelineno-38-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-38-6" name="__codelineno-38-6" href="#__codelineno-38-6"></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>
|
||||||
<a id="__codelineno-38-7" name="__codelineno-38-7" href="#__codelineno-38-7"></a> <span class="c1"># 循环中的变量占用 O(1) 空间</span>
|
<a id="__codelineno-38-7" name="__codelineno-38-7" href="#__codelineno-38-7"></a><span class="w"> </span><span class="sd">"""常数阶"""</span>
|
||||||
<a id="__codelineno-38-8" name="__codelineno-38-8" href="#__codelineno-38-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-38-8" name="__codelineno-38-8" href="#__codelineno-38-8"></a> <span class="c1"># 常量、变量、对象占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-38-9" name="__codelineno-38-9" href="#__codelineno-38-9"></a> <span class="n">c</span> <span class="o">=</span> <span class="mi">0</span>
|
<a id="__codelineno-38-9" name="__codelineno-38-9" href="#__codelineno-38-9"></a> <span class="n">a</span> <span class="o">=</span> <span class="mi">0</span>
|
||||||
<a id="__codelineno-38-10" name="__codelineno-38-10" href="#__codelineno-38-10"></a> <span class="c1"># 循环中的函数占用 O(1) 空间</span>
|
<a id="__codelineno-38-10" name="__codelineno-38-10" href="#__codelineno-38-10"></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-38-11" name="__codelineno-38-11" href="#__codelineno-38-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-38-11" name="__codelineno-38-11" href="#__codelineno-38-11"></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-38-12" name="__codelineno-38-12" href="#__codelineno-38-12"></a> <span class="n">function</span><span class="p">()</span>
|
<a id="__codelineno-38-12" name="__codelineno-38-12" href="#__codelineno-38-12"></a> <span class="c1"># 循环中的变量占用 O(1) 空间</span>
|
||||||
|
<a id="__codelineno-38-13" name="__codelineno-38-13" href="#__codelineno-38-13"></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-38-14" name="__codelineno-38-14" href="#__codelineno-38-14"></a> <span class="n">c</span> <span class="o">=</span> <span class="mi">0</span>
|
||||||
|
<a id="__codelineno-38-15" name="__codelineno-38-15" href="#__codelineno-38-15"></a> <span class="c1"># 循环中的函数占用 O(1) 空间</span>
|
||||||
|
<a id="__codelineno-38-16" name="__codelineno-38-16" href="#__codelineno-38-16"></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-38-17" name="__codelineno-38-17" href="#__codelineno-38-17"></a> <span class="n">function</span><span class="p">()</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">space_complexity.go</span><pre><span></span><code><a id="__codelineno-39-1" name="__codelineno-39-1" href="#__codelineno-39-1"></a><span class="cm">/* 常数阶 */</span>
|
<div class="highlight"><span class="filename">space_complexity.go</span><pre><span></span><code><a id="__codelineno-39-1" name="__codelineno-39-1" href="#__codelineno-39-1"></a><span class="cm">/* 函数 */</span>
|
||||||
<a id="__codelineno-39-2" name="__codelineno-39-2" href="#__codelineno-39-2"></a><span class="kd">func</span><span class="w"> </span><span class="nx">spaceConstant</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-39-2" name="__codelineno-39-2" href="#__codelineno-39-2"></a><span class="kd">func</span><span class="w"> </span><span class="nx">function</span><span class="p">()</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-39-3" name="__codelineno-39-3" href="#__codelineno-39-3"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
<a id="__codelineno-39-3" name="__codelineno-39-3" href="#__codelineno-39-3"></a><span class="w"> </span><span class="c1">// do something...</span>
|
||||||
<a id="__codelineno-39-4" name="__codelineno-39-4" href="#__codelineno-39-4"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">0</span>
|
<a id="__codelineno-39-4" name="__codelineno-39-4" href="#__codelineno-39-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span>
|
||||||
<a id="__codelineno-39-5" name="__codelineno-39-5" href="#__codelineno-39-5"></a><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">0</span>
|
<a id="__codelineno-39-5" name="__codelineno-39-5" href="#__codelineno-39-5"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-39-6" name="__codelineno-39-6" href="#__codelineno-39-6"></a><span class="w"> </span><span class="nx">nums</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">([]</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="mi">10000</span><span class="p">)</span>
|
<a id="__codelineno-39-6" name="__codelineno-39-6" href="#__codelineno-39-6"></a>
|
||||||
<a id="__codelineno-39-7" name="__codelineno-39-7" href="#__codelineno-39-7"></a><span class="w"> </span><span class="nx">ListNode</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">newNode</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
<a id="__codelineno-39-7" name="__codelineno-39-7" href="#__codelineno-39-7"></a><span class="cm">/* 常数阶 */</span>
|
||||||
<a id="__codelineno-39-8" name="__codelineno-39-8" href="#__codelineno-39-8"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
<a id="__codelineno-39-8" name="__codelineno-39-8" href="#__codelineno-39-8"></a><span class="kd">func</span><span class="w"> </span><span class="nx">spaceConstant</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-39-9" name="__codelineno-39-9" href="#__codelineno-39-9"></a><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">c</span><span class="w"> </span><span class="kt">int</span>
|
<a id="__codelineno-39-9" name="__codelineno-39-9" href="#__codelineno-39-9"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-39-10" name="__codelineno-39-10" href="#__codelineno-39-10"></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="nx">n</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>
|
<a id="__codelineno-39-10" name="__codelineno-39-10" href="#__codelineno-39-10"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">0</span>
|
||||||
<a id="__codelineno-39-11" name="__codelineno-39-11" href="#__codelineno-39-11"></a><span class="w"> </span><span class="nx">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">0</span>
|
<a id="__codelineno-39-11" name="__codelineno-39-11" href="#__codelineno-39-11"></a><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">0</span>
|
||||||
<a id="__codelineno-39-12" name="__codelineno-39-12" href="#__codelineno-39-12"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-12" name="__codelineno-39-12" href="#__codelineno-39-12"></a><span class="w"> </span><span class="nx">nums</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">([]</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="mi">10000</span><span class="p">)</span>
|
||||||
<a id="__codelineno-39-13" name="__codelineno-39-13" href="#__codelineno-39-13"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
<a id="__codelineno-39-13" name="__codelineno-39-13" href="#__codelineno-39-13"></a><span class="w"> </span><span class="nx">ListNode</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">newNode</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
||||||
<a id="__codelineno-39-14" name="__codelineno-39-14" href="#__codelineno-39-14"></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="nx">n</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>
|
<a id="__codelineno-39-14" name="__codelineno-39-14" href="#__codelineno-39-14"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-39-15" name="__codelineno-39-15" href="#__codelineno-39-15"></a><span class="w"> </span><span class="nx">function</span><span class="p">()</span>
|
<a id="__codelineno-39-15" name="__codelineno-39-15" href="#__codelineno-39-15"></a><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">c</span><span class="w"> </span><span class="kt">int</span>
|
||||||
<a id="__codelineno-39-16" name="__codelineno-39-16" href="#__codelineno-39-16"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-39-16" name="__codelineno-39-16" href="#__codelineno-39-16"></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="nx">n</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>
|
||||||
<a id="__codelineno-39-17" name="__codelineno-39-17" href="#__codelineno-39-17"></a><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="p">,</span><span class="w"> </span><span class="nx">nums</span><span class="p">,</span><span class="w"> </span><span class="nx">c</span><span class="p">,</span><span class="w"> </span><span class="nx">ListNode</span><span class="p">)</span>
|
<a id="__codelineno-39-17" name="__codelineno-39-17" href="#__codelineno-39-17"></a><span class="w"> </span><span class="nx">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">0</span>
|
||||||
<a id="__codelineno-39-18" name="__codelineno-39-18" href="#__codelineno-39-18"></a><span class="p">}</span>
|
<a id="__codelineno-39-18" name="__codelineno-39-18" href="#__codelineno-39-18"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-39-19" name="__codelineno-39-19" href="#__codelineno-39-19"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
||||||
|
<a id="__codelineno-39-20" name="__codelineno-39-20" href="#__codelineno-39-20"></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="nx">n</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>
|
||||||
|
<a id="__codelineno-39-21" name="__codelineno-39-21" href="#__codelineno-39-21"></a><span class="w"> </span><span class="nx">function</span><span class="p">()</span>
|
||||||
|
<a id="__codelineno-39-22" name="__codelineno-39-22" href="#__codelineno-39-22"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-39-23" name="__codelineno-39-23" href="#__codelineno-39-23"></a><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="p">,</span><span class="w"> </span><span class="nx">nums</span><span class="p">,</span><span class="w"> </span><span class="nx">c</span><span class="p">,</span><span class="w"> </span><span class="nx">ListNode</span><span class="p">)</span>
|
||||||
|
<a id="__codelineno-39-24" name="__codelineno-39-24" href="#__codelineno-39-24"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">space_complexity.js</span><pre><span></span><code><a id="__codelineno-40-1" name="__codelineno-40-1" href="#__codelineno-40-1"></a><span class="cm">/* 常数阶 */</span>
|
<div class="highlight"><span class="filename">space_complexity.js</span><pre><span></span><code><a id="__codelineno-40-1" name="__codelineno-40-1" href="#__codelineno-40-1"></a><span class="cm">/* 函数 */</span>
|
||||||
<a id="__codelineno-40-2" name="__codelineno-40-2" href="#__codelineno-40-2"></a><span class="kd">function</span><span class="w"> </span><span class="nx">constant</span><span class="p">(</span><span class="nx">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-40-2" name="__codelineno-40-2" href="#__codelineno-40-2"></a><span class="kd">function</span><span class="w"> </span><span class="nx">constFunc</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-40-3" name="__codelineno-40-3" href="#__codelineno-40-3"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
<a id="__codelineno-40-3" name="__codelineno-40-3" href="#__codelineno-40-3"></a><span class="w"> </span><span class="c1">// do something</span>
|
||||||
<a id="__codelineno-40-4" name="__codelineno-40-4" href="#__codelineno-40-4"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
|
<a id="__codelineno-40-4" name="__codelineno-40-4" href="#__codelineno-40-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-40-5" name="__codelineno-40-5" href="#__codelineno-40-5"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
|
<a id="__codelineno-40-5" name="__codelineno-40-5" href="#__codelineno-40-5"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-40-6" name="__codelineno-40-6" href="#__codelineno-40-6"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="mf">10000</span><span class="p">);</span>
|
<a id="__codelineno-40-6" name="__codelineno-40-6" href="#__codelineno-40-6"></a>
|
||||||
<a id="__codelineno-40-7" name="__codelineno-40-7" href="#__codelineno-40-7"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">ListNode</span><span class="p">(</span><span class="mf">0</span><span class="p">);</span>
|
<a id="__codelineno-40-7" name="__codelineno-40-7" href="#__codelineno-40-7"></a><span class="cm">/* 常数阶 */</span>
|
||||||
<a id="__codelineno-40-8" name="__codelineno-40-8" href="#__codelineno-40-8"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
<a id="__codelineno-40-8" name="__codelineno-40-8" href="#__codelineno-40-8"></a><span class="kd">function</span><span class="w"> </span><span class="nx">constant</span><span class="p">(</span><span class="nx">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-40-9" name="__codelineno-40-9" href="#__codelineno-40-9"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-40-9" name="__codelineno-40-9" href="#__codelineno-40-9"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-40-10" name="__codelineno-40-10" href="#__codelineno-40-10"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
|
<a id="__codelineno-40-10" name="__codelineno-40-10" href="#__codelineno-40-10"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-40-11" name="__codelineno-40-11" href="#__codelineno-40-11"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-40-11" name="__codelineno-40-11" href="#__codelineno-40-11"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-40-12" name="__codelineno-40-12" href="#__codelineno-40-12"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
<a id="__codelineno-40-12" name="__codelineno-40-12" href="#__codelineno-40-12"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="mf">10000</span><span class="p">);</span>
|
||||||
<a id="__codelineno-40-13" name="__codelineno-40-13" href="#__codelineno-40-13"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-40-13" name="__codelineno-40-13" href="#__codelineno-40-13"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">ListNode</span><span class="p">(</span><span class="mf">0</span><span class="p">);</span>
|
||||||
<a id="__codelineno-40-14" name="__codelineno-40-14" href="#__codelineno-40-14"></a><span class="w"> </span><span class="nx">constFunc</span><span class="p">();</span>
|
<a id="__codelineno-40-14" name="__codelineno-40-14" href="#__codelineno-40-14"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-40-15" name="__codelineno-40-15" href="#__codelineno-40-15"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-40-15" name="__codelineno-40-15" href="#__codelineno-40-15"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-40-16" name="__codelineno-40-16" href="#__codelineno-40-16"></a><span class="p">}</span>
|
<a id="__codelineno-40-16" name="__codelineno-40-16" href="#__codelineno-40-16"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-40-17" name="__codelineno-40-17" href="#__codelineno-40-17"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-40-18" name="__codelineno-40-18" href="#__codelineno-40-18"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
||||||
|
<a id="__codelineno-40-19" name="__codelineno-40-19" href="#__codelineno-40-19"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-40-20" name="__codelineno-40-20" href="#__codelineno-40-20"></a><span class="w"> </span><span class="nx">constFunc</span><span class="p">();</span>
|
||||||
|
<a id="__codelineno-40-21" name="__codelineno-40-21" href="#__codelineno-40-21"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-40-22" name="__codelineno-40-22" href="#__codelineno-40-22"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">space_complexity.ts</span><pre><span></span><code><a id="__codelineno-41-1" name="__codelineno-41-1" href="#__codelineno-41-1"></a><span class="cm">/* 常数阶 */</span>
|
<div class="highlight"><span class="filename">space_complexity.ts</span><pre><span></span><code><a id="__codelineno-41-1" name="__codelineno-41-1" href="#__codelineno-41-1"></a><span class="cm">/* 函数 */</span>
|
||||||
<a id="__codelineno-41-2" name="__codelineno-41-2" href="#__codelineno-41-2"></a><span class="kd">function</span><span class="w"> </span><span class="nx">constant</span><span class="p">(</span><span class="nx">n</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="ow">void</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-41-2" name="__codelineno-41-2" href="#__codelineno-41-2"></a><span class="kd">function</span><span class="w"> </span><span class="nx">constFunc</span><span class="p">()</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-41-3" name="__codelineno-41-3" href="#__codelineno-41-3"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
<a id="__codelineno-41-3" name="__codelineno-41-3" href="#__codelineno-41-3"></a><span class="w"> </span><span class="c1">// do something</span>
|
||||||
<a id="__codelineno-41-4" name="__codelineno-41-4" href="#__codelineno-41-4"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
|
<a id="__codelineno-41-4" name="__codelineno-41-4" href="#__codelineno-41-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-41-5" name="__codelineno-41-5" href="#__codelineno-41-5"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
|
<a id="__codelineno-41-5" name="__codelineno-41-5" href="#__codelineno-41-5"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-41-6" name="__codelineno-41-6" href="#__codelineno-41-6"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="mf">10000</span><span class="p">);</span>
|
<a id="__codelineno-41-6" name="__codelineno-41-6" href="#__codelineno-41-6"></a>
|
||||||
<a id="__codelineno-41-7" name="__codelineno-41-7" href="#__codelineno-41-7"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">ListNode</span><span class="p">(</span><span class="mf">0</span><span class="p">);</span>
|
<a id="__codelineno-41-7" name="__codelineno-41-7" href="#__codelineno-41-7"></a><span class="cm">/* 常数阶 */</span>
|
||||||
<a id="__codelineno-41-8" name="__codelineno-41-8" href="#__codelineno-41-8"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
<a id="__codelineno-41-8" name="__codelineno-41-8" href="#__codelineno-41-8"></a><span class="kd">function</span><span class="w"> </span><span class="nx">constant</span><span class="p">(</span><span class="nx">n</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="ow">void</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-41-9" name="__codelineno-41-9" href="#__codelineno-41-9"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-41-9" name="__codelineno-41-9" href="#__codelineno-41-9"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-41-10" name="__codelineno-41-10" href="#__codelineno-41-10"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
|
<a id="__codelineno-41-10" name="__codelineno-41-10" href="#__codelineno-41-10"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-41-11" name="__codelineno-41-11" href="#__codelineno-41-11"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-41-11" name="__codelineno-41-11" href="#__codelineno-41-11"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-41-12" name="__codelineno-41-12" href="#__codelineno-41-12"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
<a id="__codelineno-41-12" name="__codelineno-41-12" href="#__codelineno-41-12"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="mf">10000</span><span class="p">);</span>
|
||||||
<a id="__codelineno-41-13" name="__codelineno-41-13" href="#__codelineno-41-13"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-41-13" name="__codelineno-41-13" href="#__codelineno-41-13"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">ListNode</span><span class="p">(</span><span class="mf">0</span><span class="p">);</span>
|
||||||
<a id="__codelineno-41-14" name="__codelineno-41-14" href="#__codelineno-41-14"></a><span class="w"> </span><span class="nx">constFunc</span><span class="p">();</span>
|
<a id="__codelineno-41-14" name="__codelineno-41-14" href="#__codelineno-41-14"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-41-15" name="__codelineno-41-15" href="#__codelineno-41-15"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-41-15" name="__codelineno-41-15" href="#__codelineno-41-15"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-41-16" name="__codelineno-41-16" href="#__codelineno-41-16"></a><span class="p">}</span>
|
<a id="__codelineno-41-16" name="__codelineno-41-16" href="#__codelineno-41-16"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-41-17" name="__codelineno-41-17" href="#__codelineno-41-17"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-41-18" name="__codelineno-41-18" href="#__codelineno-41-18"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
||||||
|
<a id="__codelineno-41-19" name="__codelineno-41-19" href="#__codelineno-41-19"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-41-20" name="__codelineno-41-20" href="#__codelineno-41-20"></a><span class="w"> </span><span class="nx">constFunc</span><span class="p">();</span>
|
||||||
|
<a id="__codelineno-41-21" name="__codelineno-41-21" href="#__codelineno-41-21"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-41-22" name="__codelineno-41-22" href="#__codelineno-41-22"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">space_complexity.c</span><pre><span></span><code><a id="__codelineno-42-1" name="__codelineno-42-1" href="#__codelineno-42-1"></a><span class="cm">/* 常数阶 */</span>
|
<div class="highlight"><span class="filename">space_complexity.c</span><pre><span></span><code><a id="__codelineno-42-1" name="__codelineno-42-1" href="#__codelineno-42-1"></a><span class="cm">/* 函数 */</span>
|
||||||
<a id="__codelineno-42-2" name="__codelineno-42-2" href="#__codelineno-42-2"></a><span class="kt">void</span><span class="w"> </span><span class="nf">constant</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-42-2" name="__codelineno-42-2" href="#__codelineno-42-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">func</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-42-3" name="__codelineno-42-3" href="#__codelineno-42-3"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
<a id="__codelineno-42-3" name="__codelineno-42-3" href="#__codelineno-42-3"></a><span class="w"> </span><span class="c1">// do something</span>
|
||||||
<a id="__codelineno-42-4" name="__codelineno-42-4" href="#__codelineno-42-4"></a><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-42-4" name="__codelineno-42-4" href="#__codelineno-42-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-42-5" name="__codelineno-42-5" href="#__codelineno-42-5"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-42-5" name="__codelineno-42-5" href="#__codelineno-42-5"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-42-6" name="__codelineno-42-6" href="#__codelineno-42-6"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="mi">1000</span><span class="p">];</span>
|
<a id="__codelineno-42-6" name="__codelineno-42-6" href="#__codelineno-42-6"></a>
|
||||||
<a id="__codelineno-42-7" name="__codelineno-42-7" href="#__codelineno-42-7"></a><span class="w"> </span><span class="n">ListNode</span><span class="w"> </span><span class="o">*</span><span class="n">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">newListNode</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
<a id="__codelineno-42-7" name="__codelineno-42-7" href="#__codelineno-42-7"></a><span class="cm">/* 常数阶 */</span>
|
||||||
<a id="__codelineno-42-8" name="__codelineno-42-8" href="#__codelineno-42-8"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">node</span><span class="p">);</span>
|
<a id="__codelineno-42-8" name="__codelineno-42-8" href="#__codelineno-42-8"></a><span class="kt">void</span><span class="w"> </span><span class="nf">constant</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-42-9" name="__codelineno-42-9" href="#__codelineno-42-9"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
<a id="__codelineno-42-9" name="__codelineno-42-9" href="#__codelineno-42-9"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-42-10" name="__codelineno-42-10" href="#__codelineno-42-10"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-42-10" name="__codelineno-42-10" href="#__codelineno-42-10"></a><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-42-11" name="__codelineno-42-11" href="#__codelineno-42-11"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-42-11" name="__codelineno-42-11" href="#__codelineno-42-11"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-42-12" name="__codelineno-42-12" href="#__codelineno-42-12"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-42-12" name="__codelineno-42-12" href="#__codelineno-42-12"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">nums</span><span class="p">[</span><span class="mi">1000</span><span class="p">];</span>
|
||||||
<a id="__codelineno-42-13" name="__codelineno-42-13" href="#__codelineno-42-13"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
<a id="__codelineno-42-13" name="__codelineno-42-13" href="#__codelineno-42-13"></a><span class="w"> </span><span class="n">ListNode</span><span class="w"> </span><span class="o">*</span><span class="n">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">newListNode</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
||||||
<a id="__codelineno-42-14" name="__codelineno-42-14" href="#__codelineno-42-14"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-42-14" name="__codelineno-42-14" href="#__codelineno-42-14"></a><span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">node</span><span class="p">);</span>
|
||||||
<a id="__codelineno-42-15" name="__codelineno-42-15" href="#__codelineno-42-15"></a><span class="w"> </span><span class="n">func</span><span class="p">();</span>
|
<a id="__codelineno-42-15" name="__codelineno-42-15" href="#__codelineno-42-15"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-42-16" name="__codelineno-42-16" href="#__codelineno-42-16"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-42-16" name="__codelineno-42-16" href="#__codelineno-42-16"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-42-17" name="__codelineno-42-17" href="#__codelineno-42-17"></a><span class="p">}</span>
|
<a id="__codelineno-42-17" name="__codelineno-42-17" href="#__codelineno-42-17"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-42-18" name="__codelineno-42-18" href="#__codelineno-42-18"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-42-19" name="__codelineno-42-19" href="#__codelineno-42-19"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
||||||
|
<a id="__codelineno-42-20" name="__codelineno-42-20" href="#__codelineno-42-20"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-42-21" name="__codelineno-42-21" href="#__codelineno-42-21"></a><span class="w"> </span><span class="n">func</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="p">}</span>
|
||||||
|
<a id="__codelineno-42-23" name="__codelineno-42-23" href="#__codelineno-42-23"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">space_complexity.cs</span><pre><span></span><code><a id="__codelineno-43-1" name="__codelineno-43-1" href="#__codelineno-43-1"></a><span class="cm">/* 常数阶 */</span>
|
<div class="highlight"><span class="filename">space_complexity.cs</span><pre><span></span><code><a id="__codelineno-43-1" name="__codelineno-43-1" href="#__codelineno-43-1"></a><span class="cm">/* 函数 */</span>
|
||||||
<a id="__codelineno-43-2" name="__codelineno-43-2" href="#__codelineno-43-2"></a><span class="k">void</span><span class="w"> </span><span class="nf">constant</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-43-2" name="__codelineno-43-2" href="#__codelineno-43-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">function</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-43-3" name="__codelineno-43-3" href="#__codelineno-43-3"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
<a id="__codelineno-43-3" name="__codelineno-43-3" href="#__codelineno-43-3"></a><span class="w"> </span><span class="c1">// do something</span>
|
||||||
<a id="__codelineno-43-4" name="__codelineno-43-4" href="#__codelineno-43-4"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
<a id="__codelineno-43-4" name="__codelineno-43-4" href="#__codelineno-43-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-43-5" name="__codelineno-43-5" href="#__codelineno-43-5"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
<a id="__codelineno-43-5" name="__codelineno-43-5" href="#__codelineno-43-5"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-43-6" name="__codelineno-43-6" href="#__codelineno-43-6"></a><span class="w"> </span><span class="kt">int</span><span class="p">[]</span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">int</span><span class="p">[</span><span class="m">10000</span><span class="p">];</span>
|
<a id="__codelineno-43-6" name="__codelineno-43-6" href="#__codelineno-43-6"></a>
|
||||||
<a id="__codelineno-43-7" name="__codelineno-43-7" href="#__codelineno-43-7"></a><span class="w"> </span><span class="n">ListNode</span><span class="w"> </span><span class="n">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ListNode</span><span class="p">(</span><span class="m">0</span><span class="p">);</span>
|
<a id="__codelineno-43-7" name="__codelineno-43-7" href="#__codelineno-43-7"></a><span class="cm">/* 常数阶 */</span>
|
||||||
<a id="__codelineno-43-8" name="__codelineno-43-8" href="#__codelineno-43-8"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
<a id="__codelineno-43-8" name="__codelineno-43-8" href="#__codelineno-43-8"></a><span class="k">void</span><span class="w"> </span><span class="nf">constant</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-43-9" name="__codelineno-43-9" href="#__codelineno-43-9"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-43-9" name="__codelineno-43-9" href="#__codelineno-43-9"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-43-10" name="__codelineno-43-10" href="#__codelineno-43-10"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
<a id="__codelineno-43-10" name="__codelineno-43-10" href="#__codelineno-43-10"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-43-11" name="__codelineno-43-11" href="#__codelineno-43-11"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-43-11" name="__codelineno-43-11" href="#__codelineno-43-11"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-43-12" name="__codelineno-43-12" href="#__codelineno-43-12"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
<a id="__codelineno-43-12" name="__codelineno-43-12" href="#__codelineno-43-12"></a><span class="w"> </span><span class="kt">int</span><span class="p">[]</span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">int</span><span class="p">[</span><span class="m">10000</span><span class="p">];</span>
|
||||||
<a id="__codelineno-43-13" name="__codelineno-43-13" href="#__codelineno-43-13"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-43-13" name="__codelineno-43-13" href="#__codelineno-43-13"></a><span class="w"> </span><span class="n">ListNode</span><span class="w"> </span><span class="n">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ListNode</span><span class="p">(</span><span class="m">0</span><span class="p">);</span>
|
||||||
<a id="__codelineno-43-14" name="__codelineno-43-14" href="#__codelineno-43-14"></a><span class="w"> </span><span class="n">function</span><span class="p">();</span>
|
<a id="__codelineno-43-14" name="__codelineno-43-14" href="#__codelineno-43-14"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-43-15" name="__codelineno-43-15" href="#__codelineno-43-15"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-43-15" name="__codelineno-43-15" href="#__codelineno-43-15"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-43-16" name="__codelineno-43-16" href="#__codelineno-43-16"></a><span class="p">}</span>
|
<a id="__codelineno-43-16" name="__codelineno-43-16" href="#__codelineno-43-16"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-43-17" name="__codelineno-43-17" href="#__codelineno-43-17"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-43-18" name="__codelineno-43-18" href="#__codelineno-43-18"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
||||||
|
<a id="__codelineno-43-19" name="__codelineno-43-19" href="#__codelineno-43-19"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-43-20" name="__codelineno-43-20" href="#__codelineno-43-20"></a><span class="w"> </span><span class="n">function</span><span class="p">();</span>
|
||||||
|
<a id="__codelineno-43-21" name="__codelineno-43-21" href="#__codelineno-43-21"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-43-22" name="__codelineno-43-22" href="#__codelineno-43-22"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">space_complexity.swift</span><pre><span></span><code><a id="__codelineno-44-1" name="__codelineno-44-1" href="#__codelineno-44-1"></a><span class="cm">/* 常数阶 */</span>
|
<div class="highlight"><span class="filename">space_complexity.swift</span><pre><span></span><code><a id="__codelineno-44-1" name="__codelineno-44-1" href="#__codelineno-44-1"></a><span class="cm">/* 函数 */</span>
|
||||||
<a id="__codelineno-44-2" name="__codelineno-44-2" href="#__codelineno-44-2"></a><span class="kd">func</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="p">{</span>
|
<a id="__codelineno-44-2" name="__codelineno-44-2" href="#__codelineno-44-2"></a><span class="p">@</span><span class="n">discardableResult</span>
|
||||||
<a id="__codelineno-44-3" name="__codelineno-44-3" href="#__codelineno-44-3"></a> <span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
<a id="__codelineno-44-3" name="__codelineno-44-3" href="#__codelineno-44-3"></a><span class="kd">func</span> <span class="nf">function</span><span class="p">()</span> <span class="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
||||||
<a id="__codelineno-44-4" name="__codelineno-44-4" href="#__codelineno-44-4"></a> <span class="kd">let</span> <span class="nv">a</span> <span class="p">=</span> <span class="mi">0</span>
|
<a id="__codelineno-44-4" name="__codelineno-44-4" href="#__codelineno-44-4"></a> <span class="c1">// do something</span>
|
||||||
<a id="__codelineno-44-5" name="__codelineno-44-5" href="#__codelineno-44-5"></a> <span class="kd">var</span> <span class="nv">b</span> <span class="p">=</span> <span class="mi">0</span>
|
<a id="__codelineno-44-5" name="__codelineno-44-5" href="#__codelineno-44-5"></a> <span class="k">return</span> <span class="mi">0</span>
|
||||||
<a id="__codelineno-44-6" name="__codelineno-44-6" href="#__codelineno-44-6"></a> <span class="kd">let</span> <span class="nv">nums</span> <span class="p">=</span> <span class="nb">Array</span><span class="p">(</span><span class="n">repeating</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="bp">count</span><span class="p">:</span> <span class="mi">10000</span><span class="p">)</span>
|
<a id="__codelineno-44-6" name="__codelineno-44-6" href="#__codelineno-44-6"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-44-7" name="__codelineno-44-7" href="#__codelineno-44-7"></a> <span class="kd">let</span> <span class="nv">node</span> <span class="p">=</span> <span class="n">ListNode</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="mi">0</span><span class="p">)</span>
|
<a id="__codelineno-44-7" name="__codelineno-44-7" href="#__codelineno-44-7"></a>
|
||||||
<a id="__codelineno-44-8" name="__codelineno-44-8" href="#__codelineno-44-8"></a> <span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
<a id="__codelineno-44-8" name="__codelineno-44-8" href="#__codelineno-44-8"></a><span class="cm">/* 常数阶 */</span>
|
||||||
<a id="__codelineno-44-9" name="__codelineno-44-9" href="#__codelineno-44-9"></a> <span class="k">for</span> <span class="kc">_</span> <span class="k">in</span> <span class="mi">0</span> <span class="p">..</span><span class="o"><</span> <span class="n">n</span> <span class="p">{</span>
|
<a id="__codelineno-44-9" name="__codelineno-44-9" href="#__codelineno-44-9"></a><span class="kd">func</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="p">{</span>
|
||||||
<a id="__codelineno-44-10" name="__codelineno-44-10" href="#__codelineno-44-10"></a> <span class="kd">let</span> <span class="nv">c</span> <span class="p">=</span> <span class="mi">0</span>
|
<a id="__codelineno-44-10" name="__codelineno-44-10" href="#__codelineno-44-10"></a> <span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-44-11" name="__codelineno-44-11" href="#__codelineno-44-11"></a> <span class="p">}</span>
|
<a id="__codelineno-44-11" name="__codelineno-44-11" href="#__codelineno-44-11"></a> <span class="kd">let</span> <span class="nv">a</span> <span class="p">=</span> <span class="mi">0</span>
|
||||||
<a id="__codelineno-44-12" name="__codelineno-44-12" href="#__codelineno-44-12"></a> <span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
<a id="__codelineno-44-12" name="__codelineno-44-12" href="#__codelineno-44-12"></a> <span class="kd">var</span> <span class="nv">b</span> <span class="p">=</span> <span class="mi">0</span>
|
||||||
<a id="__codelineno-44-13" name="__codelineno-44-13" href="#__codelineno-44-13"></a> <span class="k">for</span> <span class="kc">_</span> <span class="k">in</span> <span class="mi">0</span> <span class="p">..</span><span class="o"><</span> <span class="n">n</span> <span class="p">{</span>
|
<a id="__codelineno-44-13" name="__codelineno-44-13" href="#__codelineno-44-13"></a> <span class="kd">let</span> <span class="nv">nums</span> <span class="p">=</span> <span class="nb">Array</span><span class="p">(</span><span class="n">repeating</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="bp">count</span><span class="p">:</span> <span class="mi">10000</span><span class="p">)</span>
|
||||||
<a id="__codelineno-44-14" name="__codelineno-44-14" href="#__codelineno-44-14"></a> <span class="n">function</span><span class="p">()</span>
|
<a id="__codelineno-44-14" name="__codelineno-44-14" href="#__codelineno-44-14"></a> <span class="kd">let</span> <span class="nv">node</span> <span class="p">=</span> <span class="n">ListNode</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="mi">0</span><span class="p">)</span>
|
||||||
<a id="__codelineno-44-15" name="__codelineno-44-15" href="#__codelineno-44-15"></a> <span class="p">}</span>
|
<a id="__codelineno-44-15" name="__codelineno-44-15" href="#__codelineno-44-15"></a> <span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-44-16" name="__codelineno-44-16" href="#__codelineno-44-16"></a><span class="p">}</span>
|
<a id="__codelineno-44-16" name="__codelineno-44-16" href="#__codelineno-44-16"></a> <span class="k">for</span> <span class="kc">_</span> <span class="k">in</span> <span class="mi">0</span> <span class="p">..</span><span class="o"><</span> <span class="n">n</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-44-17" name="__codelineno-44-17" href="#__codelineno-44-17"></a> <span class="kd">let</span> <span class="nv">c</span> <span class="p">=</span> <span class="mi">0</span>
|
||||||
|
<a id="__codelineno-44-18" name="__codelineno-44-18" href="#__codelineno-44-18"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-44-19" name="__codelineno-44-19" href="#__codelineno-44-19"></a> <span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
||||||
|
<a id="__codelineno-44-20" name="__codelineno-44-20" href="#__codelineno-44-20"></a> <span class="k">for</span> <span class="kc">_</span> <span class="k">in</span> <span class="mi">0</span> <span class="p">..</span><span class="o"><</span> <span class="n">n</span> <span class="p">{</span>
|
||||||
|
<a id="__codelineno-44-21" name="__codelineno-44-21" href="#__codelineno-44-21"></a> <span class="n">function</span><span class="p">()</span>
|
||||||
|
<a id="__codelineno-44-22" name="__codelineno-44-22" href="#__codelineno-44-22"></a> <span class="p">}</span>
|
||||||
|
<a id="__codelineno-44-23" name="__codelineno-44-23" href="#__codelineno-44-23"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">space_complexity.zig</span><pre><span></span><code><a id="__codelineno-45-1" name="__codelineno-45-1" href="#__codelineno-45-1"></a><span class="c1">// 常数阶</span>
|
<div class="highlight"><span class="filename">space_complexity.zig</span><pre><span></span><code><a id="__codelineno-45-1" name="__codelineno-45-1" href="#__codelineno-45-1"></a><span class="p">[</span><span class="n">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="n">func</span><span class="p">]{</span><span class="n">function</span><span class="p">}</span>
|
||||||
<a id="__codelineno-45-2" name="__codelineno-45-2" href="#__codelineno-45-2"></a><span class="k">fn</span><span class="w"> </span><span class="n">constant</span><span class="p">(</span><span class="n">n</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-45-2" name="__codelineno-45-2" href="#__codelineno-45-2"></a>
|
||||||
<a id="__codelineno-45-3" name="__codelineno-45-3" href="#__codelineno-45-3"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
<a id="__codelineno-45-3" name="__codelineno-45-3" href="#__codelineno-45-3"></a><span class="c1">// 常数阶</span>
|
||||||
<a id="__codelineno-45-4" name="__codelineno-45-4" href="#__codelineno-45-4"></a><span class="w"> </span><span class="kr">const</span><span class="w"> </span><span class="n">a</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-45-4" name="__codelineno-45-4" href="#__codelineno-45-4"></a><span class="k">fn</span><span class="w"> </span><span class="n">constant</span><span class="p">(</span><span class="n">n</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-45-5" name="__codelineno-45-5" href="#__codelineno-45-5"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">b</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-45-5" name="__codelineno-45-5" href="#__codelineno-45-5"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-45-6" name="__codelineno-45-6" href="#__codelineno-45-6"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="n">_</span><span class="p">]</span><span class="kt">i32</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="p">;</span>
|
<a id="__codelineno-45-6" name="__codelineno-45-6" href="#__codelineno-45-6"></a><span class="w"> </span><span class="kr">const</span><span class="w"> </span><span class="n">a</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-45-7" name="__codelineno-45-7" href="#__codelineno-45-7"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">inc</span><span class="p">.</span><span class="n">ListNode</span><span class="p">(</span><span class="kt">i32</span><span class="p">){.</span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">};</span>
|
<a id="__codelineno-45-7" name="__codelineno-45-7" href="#__codelineno-45-7"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">b</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-45-8" name="__codelineno-45-8" href="#__codelineno-45-8"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">i</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-45-8" name="__codelineno-45-8" href="#__codelineno-45-8"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="n">_</span><span class="p">]</span><span class="kt">i32</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="p">;</span>
|
||||||
<a id="__codelineno-45-9" name="__codelineno-45-9" href="#__codelineno-45-9"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
<a id="__codelineno-45-9" name="__codelineno-45-9" href="#__codelineno-45-9"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">inc</span><span class="p">.</span><span class="n">ListNode</span><span class="p">(</span><span class="kt">i32</span><span class="p">){.</span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">};</span>
|
||||||
<a id="__codelineno-45-10" name="__codelineno-45-10" href="#__codelineno-45-10"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="p">(</span><span class="n">i</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="p">{</span>
|
<a id="__codelineno-45-10" name="__codelineno-45-10" href="#__codelineno-45-10"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">i</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-45-11" name="__codelineno-45-11" href="#__codelineno-45-11"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">c</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-45-11" name="__codelineno-45-11" href="#__codelineno-45-11"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-45-12" name="__codelineno-45-12" href="#__codelineno-45-12"></a><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">c</span><span class="p">;</span>
|
<a id="__codelineno-45-12" name="__codelineno-45-12" href="#__codelineno-45-12"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="p">(</span><span class="n">i</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="p">{</span>
|
||||||
<a id="__codelineno-45-13" name="__codelineno-45-13" href="#__codelineno-45-13"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-45-13" name="__codelineno-45-13" href="#__codelineno-45-13"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">c</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-45-14" name="__codelineno-45-14" href="#__codelineno-45-14"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
<a id="__codelineno-45-14" name="__codelineno-45-14" href="#__codelineno-45-14"></a><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">c</span><span class="p">;</span>
|
||||||
<a id="__codelineno-45-15" name="__codelineno-45-15" href="#__codelineno-45-15"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-45-15" name="__codelineno-45-15" href="#__codelineno-45-15"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-45-16" name="__codelineno-45-16" href="#__codelineno-45-16"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="p">(</span><span class="n">i</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="p">{</span>
|
<a id="__codelineno-45-16" name="__codelineno-45-16" href="#__codelineno-45-16"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-45-17" name="__codelineno-45-17" href="#__codelineno-45-17"></a><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">function</span><span class="p">();</span>
|
<a id="__codelineno-45-17" name="__codelineno-45-17" href="#__codelineno-45-17"></a><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-45-18" name="__codelineno-45-18" href="#__codelineno-45-18"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-45-18" name="__codelineno-45-18" href="#__codelineno-45-18"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="p">(</span><span class="n">i</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="p">{</span>
|
||||||
<a id="__codelineno-45-19" name="__codelineno-45-19" href="#__codelineno-45-19"></a><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">;</span>
|
<a id="__codelineno-45-19" name="__codelineno-45-19" href="#__codelineno-45-19"></a><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">function</span><span class="p">();</span>
|
||||||
<a id="__codelineno-45-20" name="__codelineno-45-20" href="#__codelineno-45-20"></a><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
|
<a id="__codelineno-45-20" name="__codelineno-45-20" href="#__codelineno-45-20"></a><span class="w"> </span><span class="p">}</span>
|
||||||
<a id="__codelineno-45-21" name="__codelineno-45-21" href="#__codelineno-45-21"></a><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="p">;</span>
|
<a id="__codelineno-45-21" name="__codelineno-45-21" href="#__codelineno-45-21"></a><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">;</span>
|
||||||
<a id="__codelineno-45-22" name="__codelineno-45-22" href="#__codelineno-45-22"></a><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">node</span><span class="p">;</span>
|
<a id="__codelineno-45-22" name="__codelineno-45-22" href="#__codelineno-45-22"></a><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
|
||||||
<a id="__codelineno-45-23" name="__codelineno-45-23" href="#__codelineno-45-23"></a><span class="p">}</span>
|
<a id="__codelineno-45-23" name="__codelineno-45-23" href="#__codelineno-45-23"></a><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-45-24" name="__codelineno-45-24" href="#__codelineno-45-24"></a><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">node</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-45-25" name="__codelineno-45-25" href="#__codelineno-45-25"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">space_complexity.dart</span><pre><span></span><code><a id="__codelineno-46-1" name="__codelineno-46-1" href="#__codelineno-46-1"></a><span class="cm">/* 常数阶 */</span>
|
<div class="highlight"><span class="filename">space_complexity.dart</span><pre><span></span><code><a id="__codelineno-46-1" name="__codelineno-46-1" href="#__codelineno-46-1"></a><span class="cm">/* 函数 */</span>
|
||||||
<a id="__codelineno-46-2" name="__codelineno-46-2" href="#__codelineno-46-2"></a><span class="kt">void</span><span class="w"> </span><span class="n">constant</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-46-2" name="__codelineno-46-2" href="#__codelineno-46-2"></a><span class="kt">int</span><span class="w"> </span><span class="n">function</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-46-3" name="__codelineno-46-3" href="#__codelineno-46-3"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
<a id="__codelineno-46-3" name="__codelineno-46-3" href="#__codelineno-46-3"></a><span class="w"> </span><span class="c1">// do something</span>
|
||||||
<a id="__codelineno-46-4" name="__codelineno-46-4" href="#__codelineno-46-4"></a><span class="w"> </span><span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
<a id="__codelineno-46-4" name="__codelineno-46-4" href="#__codelineno-46-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-46-5" name="__codelineno-46-5" href="#__codelineno-46-5"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
<a id="__codelineno-46-5" name="__codelineno-46-5" href="#__codelineno-46-5"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-46-6" name="__codelineno-46-6" href="#__codelineno-46-6"></a>
|
<a id="__codelineno-46-6" name="__codelineno-46-6" href="#__codelineno-46-6"></a>
|
||||||
<a id="__codelineno-46-7" name="__codelineno-46-7" href="#__codelineno-46-7"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">List</span><span class="p">.</span><span class="n">filled</span><span class="p">(</span><span class="m">10000</span><span class="p">,</span><span class="w"> </span><span class="m">0</span><span class="p">);</span>
|
<a id="__codelineno-46-7" name="__codelineno-46-7" href="#__codelineno-46-7"></a><span class="cm">/* 常数阶 */</span>
|
||||||
<a id="__codelineno-46-8" name="__codelineno-46-8" href="#__codelineno-46-8"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
<a id="__codelineno-46-8" name="__codelineno-46-8" href="#__codelineno-46-8"></a><span class="kt">void</span><span class="w"> </span><span class="n">constant</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-46-9" name="__codelineno-46-9" href="#__codelineno-46-9"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-46-9" name="__codelineno-46-9" href="#__codelineno-46-9"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-46-10" name="__codelineno-46-10" href="#__codelineno-46-10"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
<a id="__codelineno-46-10" name="__codelineno-46-10" href="#__codelineno-46-10"></a><span class="w"> </span><span class="kd">final</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-46-11" name="__codelineno-46-11" href="#__codelineno-46-11"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-46-11" name="__codelineno-46-11" href="#__codelineno-46-11"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-46-12" name="__codelineno-46-12" href="#__codelineno-46-12"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
<a id="__codelineno-46-12" name="__codelineno-46-12" href="#__codelineno-46-12"></a>
|
||||||
<a id="__codelineno-46-13" name="__codelineno-46-13" href="#__codelineno-46-13"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-46-13" name="__codelineno-46-13" href="#__codelineno-46-13"></a><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">List</span><span class="p">.</span><span class="n">filled</span><span class="p">(</span><span class="m">10000</span><span class="p">,</span><span class="w"> </span><span class="m">0</span><span class="p">);</span>
|
||||||
<a id="__codelineno-46-14" name="__codelineno-46-14" href="#__codelineno-46-14"></a><span class="w"> </span><span class="n">function</span><span class="p">();</span>
|
<a id="__codelineno-46-14" name="__codelineno-46-14" href="#__codelineno-46-14"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-46-15" name="__codelineno-46-15" href="#__codelineno-46-15"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-46-15" name="__codelineno-46-15" href="#__codelineno-46-15"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-46-16" name="__codelineno-46-16" href="#__codelineno-46-16"></a><span class="p">}</span>
|
<a id="__codelineno-46-16" name="__codelineno-46-16" href="#__codelineno-46-16"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-46-17" name="__codelineno-46-17" href="#__codelineno-46-17"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-46-18" name="__codelineno-46-18" href="#__codelineno-46-18"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
||||||
|
<a id="__codelineno-46-19" name="__codelineno-46-19" href="#__codelineno-46-19"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-46-20" name="__codelineno-46-20" href="#__codelineno-46-20"></a><span class="w"> </span><span class="n">function</span><span class="p">();</span>
|
||||||
|
<a id="__codelineno-46-21" name="__codelineno-46-21" href="#__codelineno-46-21"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-46-22" name="__codelineno-46-22" href="#__codelineno-46-22"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">space_complexity.rs</span><pre><span></span><code><a id="__codelineno-47-1" name="__codelineno-47-1" href="#__codelineno-47-1"></a><span class="cm">/* 常数阶 */</span>
|
<div class="highlight"><span class="filename">space_complexity.rs</span><pre><span></span><code><a id="__codelineno-47-1" name="__codelineno-47-1" href="#__codelineno-47-1"></a><span class="cm">/* 函数 */</span>
|
||||||
<a id="__codelineno-47-2" name="__codelineno-47-2" href="#__codelineno-47-2"></a><span class="cp">#[allow(unused)]</span>
|
<a id="__codelineno-47-2" name="__codelineno-47-2" href="#__codelineno-47-2"></a><span class="k">fn</span> <span class="nf">function</span><span class="p">()</span><span class="w"> </span>-><span class="kt">i32</span> <span class="p">{</span>
|
||||||
<a id="__codelineno-47-3" name="__codelineno-47-3" href="#__codelineno-47-3"></a><span class="k">fn</span> <span class="nf">constant</span><span class="p">(</span><span class="n">n</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-47-3" name="__codelineno-47-3" href="#__codelineno-47-3"></a><span class="w"> </span><span class="c1">// do something</span>
|
||||||
<a id="__codelineno-47-4" name="__codelineno-47-4" href="#__codelineno-47-4"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
<a id="__codelineno-47-4" name="__codelineno-47-4" href="#__codelineno-47-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-47-5" name="__codelineno-47-5" href="#__codelineno-47-5"></a><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">A</span>: <span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-47-5" name="__codelineno-47-5" href="#__codelineno-47-5"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-47-6" name="__codelineno-47-6" href="#__codelineno-47-6"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-47-6" name="__codelineno-47-6" href="#__codelineno-47-6"></a>
|
||||||
<a id="__codelineno-47-7" name="__codelineno-47-7" href="#__codelineno-47-7"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">10000</span><span class="p">];</span>
|
<a id="__codelineno-47-7" name="__codelineno-47-7" href="#__codelineno-47-7"></a><span class="cm">/* 常数阶 */</span>
|
||||||
<a id="__codelineno-47-8" name="__codelineno-47-8" href="#__codelineno-47-8"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ListNode</span>::<span class="n">new</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
<a id="__codelineno-47-8" name="__codelineno-47-8" href="#__codelineno-47-8"></a><span class="cp">#[allow(unused)]</span>
|
||||||
<a id="__codelineno-47-9" name="__codelineno-47-9" href="#__codelineno-47-9"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
<a id="__codelineno-47-9" name="__codelineno-47-9" href="#__codelineno-47-9"></a><span class="k">fn</span> <span class="nf">constant</span><span class="p">(</span><span class="n">n</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-47-10" name="__codelineno-47-10" href="#__codelineno-47-10"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="n">n</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-47-10" name="__codelineno-47-10" href="#__codelineno-47-10"></a><span class="w"> </span><span class="c1">// 常量、变量、对象占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-47-11" name="__codelineno-47-11" href="#__codelineno-47-11"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-47-11" name="__codelineno-47-11" href="#__codelineno-47-11"></a><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">A</span>: <span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-47-12" name="__codelineno-47-12" href="#__codelineno-47-12"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-47-12" name="__codelineno-47-12" href="#__codelineno-47-12"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-47-13" name="__codelineno-47-13" href="#__codelineno-47-13"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
<a id="__codelineno-47-13" name="__codelineno-47-13" href="#__codelineno-47-13"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">10000</span><span class="p">];</span>
|
||||||
<a id="__codelineno-47-14" name="__codelineno-47-14" href="#__codelineno-47-14"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="n">n</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-47-14" name="__codelineno-47-14" href="#__codelineno-47-14"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">node</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ListNode</span>::<span class="n">new</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
||||||
<a id="__codelineno-47-15" name="__codelineno-47-15" href="#__codelineno-47-15"></a><span class="w"> </span><span class="n">function</span><span class="p">();</span>
|
<a id="__codelineno-47-15" name="__codelineno-47-15" href="#__codelineno-47-15"></a><span class="w"> </span><span class="c1">// 循环中的变量占用 O(1) 空间</span>
|
||||||
<a id="__codelineno-47-16" name="__codelineno-47-16" href="#__codelineno-47-16"></a><span class="w"> </span><span class="p">}</span>
|
<a id="__codelineno-47-16" name="__codelineno-47-16" href="#__codelineno-47-16"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="n">n</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-47-17" name="__codelineno-47-17" href="#__codelineno-47-17"></a><span class="p">}</span>
|
<a id="__codelineno-47-17" name="__codelineno-47-17" href="#__codelineno-47-17"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
|
<a id="__codelineno-47-18" name="__codelineno-47-18" href="#__codelineno-47-18"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-47-19" name="__codelineno-47-19" href="#__codelineno-47-19"></a><span class="w"> </span><span class="c1">// 循环中的函数占用 O(1) 空间</span>
|
||||||
|
<a id="__codelineno-47-20" name="__codelineno-47-20" href="#__codelineno-47-20"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="n">n</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-47-21" name="__codelineno-47-21" href="#__codelineno-47-21"></a><span class="w"> </span><span class="n">function</span><span class="p">();</span>
|
||||||
|
<a id="__codelineno-47-22" name="__codelineno-47-22" href="#__codelineno-47-22"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-47-23" name="__codelineno-47-23" href="#__codelineno-47-23"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -4404,9 +4477,9 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
||||||
<a id="__codelineno-50-3" name="__codelineno-50-3" href="#__codelineno-50-3"></a> <span class="c1"># 长度为 n 的列表占用 O(n) 空间</span>
|
<a id="__codelineno-50-3" name="__codelineno-50-3" href="#__codelineno-50-3"></a> <span class="c1"># 长度为 n 的列表占用 O(n) 空间</span>
|
||||||
<a id="__codelineno-50-4" name="__codelineno-50-4" href="#__codelineno-50-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-50-4" name="__codelineno-50-4" href="#__codelineno-50-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-50-5" name="__codelineno-50-5" href="#__codelineno-50-5"></a> <span class="c1"># 长度为 n 的哈希表占用 O(n) 空间</span>
|
<a id="__codelineno-50-5" name="__codelineno-50-5" href="#__codelineno-50-5"></a> <span class="c1"># 长度为 n 的哈希表占用 O(n) 空间</span>
|
||||||
<a id="__codelineno-50-6" name="__codelineno-50-6" href="#__codelineno-50-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-50-6" name="__codelineno-50-6" href="#__codelineno-50-6"></a> <span class="n">hmap</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-50-7" name="__codelineno-50-7" href="#__codelineno-50-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>
|
<a id="__codelineno-50-7" name="__codelineno-50-7" href="#__codelineno-50-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>
|
||||||
<a id="__codelineno-50-8" name="__codelineno-50-8" href="#__codelineno-50-8"></a> <span class="n">mapp</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
<a id="__codelineno-50-8" name="__codelineno-50-8" href="#__codelineno-50-8"></a> <span class="n">hmap</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
|
|
@ -3437,8 +3437,9 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="admonition question">
|
<div class="admonition question">
|
||||||
<p class="admonition-title">图片“空间复杂度的常见类型”反映的是否是占用空间的绝对大小?</p>
|
<p class="admonition-title">图片“空间复杂度的常见类型”反映的是否是占用空间的绝对大小?</p>
|
||||||
<p>不是,该图片展示的是空间复杂度(即增长趋势),而不是占用空间的绝对大小。每个曲线都包含一个常数项,用来把所有曲线的取值范围压缩到一个视觉舒适的范围内。</p>
|
<p>不是,该图片展示的是空间复杂度,其反映的是即增长趋势,而不是占用空间的绝对大小。</p>
|
||||||
<p>实际中,因为我们通常不知道每个方法的“常数项”复杂度是多少,所以一般无法仅凭复杂度来选择 <span class="arithmatex">\(n = 8\)</span> 之下的最优解法;但相对地 <span class="arithmatex">\(n = 8^5\)</span> 就很好选了,这是复杂度占主导的情况。</p>
|
<p>假设取 <span class="arithmatex">\(n = 8\)</span> ,你可能会发现每条曲线的值与函数对应不上。这是因为每条曲线都包含一个常数项,用于将取值范围压缩到一个视觉舒适的范围内。</p>
|
||||||
|
<p>在实际中,因为我们通常不知道每个方法的“常数项”复杂度是多少,所以一般无法仅凭复杂度来选择 <span class="arithmatex">\(n = 8\)</span> 之下的最优解法。但对于 <span class="arithmatex">\(n = 8^5\)</span> 就很好选了,这时增长趋势已经占主导了。</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -641,31 +641,24 @@
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
<a href="#221" class="md-nav__link">
|
<a href="#221" class="md-nav__link">
|
||||||
2.2.1. 统计算法运行时间
|
2.2.1. 统计时间增长趋势
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
<a href="#222" class="md-nav__link">
|
<a href="#222" class="md-nav__link">
|
||||||
2.2.2. 统计时间增长趋势
|
2.2.2. 函数渐近上界
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
<a href="#223" class="md-nav__link">
|
<a href="#223" class="md-nav__link">
|
||||||
2.2.3. 函数渐近上界
|
2.2.3. 推算方法
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</li>
|
<nav class="md-nav" aria-label="2.2.3. 推算方法">
|
||||||
|
|
||||||
<li class="md-nav__item">
|
|
||||||
<a href="#224" class="md-nav__link">
|
|
||||||
2.2.4. 推算方法
|
|
||||||
</a>
|
|
||||||
|
|
||||||
<nav class="md-nav" aria-label="2.2.4. 推算方法">
|
|
||||||
<ul class="md-nav__list">
|
<ul class="md-nav__list">
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
|
@ -688,11 +681,11 @@
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
<a href="#225" class="md-nav__link">
|
<a href="#224" class="md-nav__link">
|
||||||
2.2.5. 常见类型
|
2.2.4. 常见类型
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<nav class="md-nav" aria-label="2.2.5. 常见类型">
|
<nav class="md-nav" aria-label="2.2.4. 常见类型">
|
||||||
<ul class="md-nav__list">
|
<ul class="md-nav__list">
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
|
@ -750,8 +743,8 @@
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
<a href="#226" class="md-nav__link">
|
<a href="#225" class="md-nav__link">
|
||||||
2.2.6. 最差、最佳、平均时间复杂度
|
2.2.5. 最差、最佳、平均时间复杂度
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</li>
|
</li>
|
||||||
|
@ -3479,31 +3472,24 @@
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
<a href="#221" class="md-nav__link">
|
<a href="#221" class="md-nav__link">
|
||||||
2.2.1. 统计算法运行时间
|
2.2.1. 统计时间增长趋势
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
<a href="#222" class="md-nav__link">
|
<a href="#222" class="md-nav__link">
|
||||||
2.2.2. 统计时间增长趋势
|
2.2.2. 函数渐近上界
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
<a href="#223" class="md-nav__link">
|
<a href="#223" class="md-nav__link">
|
||||||
2.2.3. 函数渐近上界
|
2.2.3. 推算方法
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</li>
|
<nav class="md-nav" aria-label="2.2.3. 推算方法">
|
||||||
|
|
||||||
<li class="md-nav__item">
|
|
||||||
<a href="#224" class="md-nav__link">
|
|
||||||
2.2.4. 推算方法
|
|
||||||
</a>
|
|
||||||
|
|
||||||
<nav class="md-nav" aria-label="2.2.4. 推算方法">
|
|
||||||
<ul class="md-nav__list">
|
<ul class="md-nav__list">
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
|
@ -3526,11 +3512,11 @@
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
<a href="#225" class="md-nav__link">
|
<a href="#224" class="md-nav__link">
|
||||||
2.2.5. 常见类型
|
2.2.4. 常见类型
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<nav class="md-nav" aria-label="2.2.5. 常见类型">
|
<nav class="md-nav" aria-label="2.2.4. 常见类型">
|
||||||
<ul class="md-nav__list">
|
<ul class="md-nav__list">
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
|
@ -3588,8 +3574,8 @@
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
<li class="md-nav__item">
|
<li class="md-nav__item">
|
||||||
<a href="#226" class="md-nav__link">
|
<a href="#225" class="md-nav__link">
|
||||||
2.2.6. 最差、最佳、平均时间复杂度
|
2.2.5. 最差、最佳、平均时间复杂度
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</li>
|
</li>
|
||||||
|
@ -3618,14 +3604,13 @@
|
||||||
|
|
||||||
|
|
||||||
<h1 id="22">2.2. 时间复杂度<a class="headerlink" href="#22" title="Permanent link">¶</a></h1>
|
<h1 id="22">2.2. 时间复杂度<a class="headerlink" href="#22" title="Permanent link">¶</a></h1>
|
||||||
<h2 id="221">2.2.1. 统计算法运行时间<a class="headerlink" href="#221" title="Permanent link">¶</a></h2>
|
<p>运行时间可以直观且准确地反映算法的效率。如果我们想要准确预估一段代码的运行时间,应该如何操作呢?</p>
|
||||||
<p>运行时间可以直观且准确地反映算法的效率。然而,如果我们想要准确预估一段代码的运行时间,应该如何操作呢?</p>
|
|
||||||
<ol>
|
<ol>
|
||||||
<li><strong>确定运行平台</strong>,包括硬件配置、编程语言、系统环境等,这些因素都会影响代码的运行效率。</li>
|
<li><strong>确定运行平台</strong>,包括硬件配置、编程语言、系统环境等,这些因素都会影响代码的运行效率。</li>
|
||||||
<li><strong>评估各种计算操作所需的运行时间</strong>,例如加法操作 <code>+</code> 需要 1 ns,乘法操作 <code>*</code> 需要 10 ns,打印操作需要 5 ns 等。</li>
|
<li><strong>评估各种计算操作所需的运行时间</strong>,例如加法操作 <code>+</code> 需要 1 ns,乘法操作 <code>*</code> 需要 10 ns,打印操作需要 5 ns 等。</li>
|
||||||
<li><strong>统计代码中所有的计算操作</strong>,并将所有操作的执行时间求和,从而得到运行时间。</li>
|
<li><strong>统计代码中所有的计算操作</strong>,并将所有操作的执行时间求和,从而得到运行时间。</li>
|
||||||
</ol>
|
</ol>
|
||||||
<p>例如以下代码,输入数据大小为 <span class="arithmatex">\(n\)</span> ,根据以上方法,可以得到算法运行时间为 <span class="arithmatex">\(6n + 12\)</span> ns 。</p>
|
<p>例如以下代码,输入数据大小为 <span class="arithmatex">\(n\)</span> 。根据以上方法,可以得到算法运行时间为 <span class="arithmatex">\(6n + 12\)</span> ns 。</p>
|
||||||
<div class="arithmatex">\[
|
<div class="arithmatex">\[
|
||||||
1 + 1 + 10 + (1 + 5) \times n = 6n + 12
|
1 + 1 + 10 + (1 + 5) \times n = 6n + 12
|
||||||
\]</div>
|
\]</div>
|
||||||
|
@ -3764,20 +3749,24 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a>
|
<div class="highlight"><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a><span class="c1">// 在某运行平台下</span>
|
||||||
|
<a id="__codelineno-11-2" name="__codelineno-11-2" href="#__codelineno-11-2"></a><span class="k">fn</span> <span class="nf">algorithm</span><span class="p">(</span><span class="n">n</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-11-3" name="__codelineno-11-3" href="#__codelineno-11-3"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// 1 ns</span>
|
||||||
|
<a id="__codelineno-11-4" name="__codelineno-11-4" href="#__codelineno-11-4"></a><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</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="c1">// 1 ns</span>
|
||||||
|
<a id="__codelineno-11-5" name="__codelineno-11-5" href="#__codelineno-11-5"></a><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// 10 ns</span>
|
||||||
|
<a id="__codelineno-11-6" name="__codelineno-11-6" href="#__codelineno-11-6"></a><span class="w"> </span><span class="c1">// 循环 n 次</span>
|
||||||
|
<a id="__codelineno-11-7" name="__codelineno-11-7" href="#__codelineno-11-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="n">n</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// 1 ns ,每轮都要执行 i++</span>
|
||||||
|
<a id="__codelineno-11-8" name="__codelineno-11-8" href="#__codelineno-11-8"></a><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span><span class="w"> </span><span class="c1">// 5 ns</span>
|
||||||
|
<a id="__codelineno-11-9" name="__codelineno-11-9" href="#__codelineno-11-9"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-11-10" name="__codelineno-11-10" href="#__codelineno-11-10"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p>然而实际上,<strong>统计算法的运行时间既不合理也不现实</strong>。首先,我们不希望预估时间和运行平台绑定,因为算法需要在各种不同的平台上运行。其次,我们很难获知每种操作的运行时间,这给预估过程带来了极大的难度。</p>
|
<p>但实际上,<strong>统计算法的运行时间既不合理也不现实</strong>。首先,我们不希望预估时间和运行平台绑定,因为算法需要在各种不同的平台上运行。其次,我们很难获知每种操作的运行时间,这给预估过程带来了极大的难度。</p>
|
||||||
<h2 id="222">2.2.2. 统计时间增长趋势<a class="headerlink" href="#222" title="Permanent link">¶</a></h2>
|
<h2 id="221">2.2.1. 统计时间增长趋势<a class="headerlink" href="#221" title="Permanent link">¶</a></h2>
|
||||||
<p>「时间复杂度分析」采取了一种不同的方法,其统计的不是算法运行时间,<strong>而是算法运行时间随着数据量变大时的增长趋势</strong>。</p>
|
<p>「时间复杂度分析」采取了一种不同的方法,其统计的不是算法运行时间,<strong>而是算法运行时间随着数据量变大时的增长趋势</strong>。</p>
|
||||||
<p>“时间增长趋势”这个概念较为抽象,我们通过一个例子来加以理解。假设输入数据大小为 <span class="arithmatex">\(n\)</span> ,给定三个算法 <code>A</code> , <code>B</code> , <code>C</code> 。</p>
|
<p>“时间增长趋势”这个概念比较抽象,我们通过一个例子来加以理解。假设输入数据大小为 <span class="arithmatex">\(n\)</span> ,给定三个算法函数 <code>A</code> , <code>B</code> , <code>C</code> 。</p>
|
||||||
<ul>
|
|
||||||
<li>算法 <code>A</code> 只有 <span class="arithmatex">\(1\)</span> 个打印操作,算法运行时间不随着 <span class="arithmatex">\(n\)</span> 增大而增长。我们称此算法的时间复杂度为「常数阶」。</li>
|
|
||||||
<li>算法 <code>B</code> 中的打印操作需要循环 <span class="arithmatex">\(n\)</span> 次,算法运行时间随着 <span class="arithmatex">\(n\)</span> 增大呈线性增长。此算法的时间复杂度被称为「线性阶」。</li>
|
|
||||||
<li>算法 <code>C</code> 中的打印操作需要循环 <span class="arithmatex">\(1000000\)</span> 次,但运行时间仍与输入数据大小 <span class="arithmatex">\(n\)</span> 无关。因此 <code>C</code> 的时间复杂度和 <code>A</code> 相同,仍为「常数阶」。</li>
|
|
||||||
</ul>
|
|
||||||
<div class="tabbed-set tabbed-alternate" data-tabs="2:12"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><input id="__tabbed_2_12" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Java</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Python</label><label for="__tabbed_2_4">Go</label><label for="__tabbed_2_5">JS</label><label for="__tabbed_2_6">TS</label><label for="__tabbed_2_7">C</label><label for="__tabbed_2_8">C#</label><label for="__tabbed_2_9">Swift</label><label for="__tabbed_2_10">Zig</label><label for="__tabbed_2_11">Dart</label><label for="__tabbed_2_12">Rust</label></div>
|
<div class="tabbed-set tabbed-alternate" data-tabs="2:12"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><input id="__tabbed_2_12" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Java</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Python</label><label for="__tabbed_2_4">Go</label><label for="__tabbed_2_5">JS</label><label for="__tabbed_2_6">TS</label><label for="__tabbed_2_7">C</label><label for="__tabbed_2_8">C#</label><label for="__tabbed_2_9">Swift</label><label for="__tabbed_2_10">Zig</label><label for="__tabbed_2_11">Dart</label><label for="__tabbed_2_12">Rust</label></div>
|
||||||
<div class="tabbed-content">
|
<div class="tabbed-content">
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -3972,23 +3961,38 @@
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><pre><span></span><code><a id="__codelineno-23-1" name="__codelineno-23-1" href="#__codelineno-23-1"></a>
|
<div class="highlight"><pre><span></span><code><a id="__codelineno-23-1" name="__codelineno-23-1" href="#__codelineno-23-1"></a><span class="c1">// 算法 A 时间复杂度:常数阶</span>
|
||||||
|
<a id="__codelineno-23-2" name="__codelineno-23-2" href="#__codelineno-23-2"></a><span class="k">fn</span> <span class="nf">algorithm_A</span><span class="p">(</span><span class="n">n</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-23-3" name="__codelineno-23-3" href="#__codelineno-23-3"></a><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-23-4" name="__codelineno-23-4" href="#__codelineno-23-4"></a><span class="p">}</span>
|
||||||
|
<a id="__codelineno-23-5" name="__codelineno-23-5" href="#__codelineno-23-5"></a><span class="c1">// 算法 B 时间复杂度:线性阶</span>
|
||||||
|
<a id="__codelineno-23-6" name="__codelineno-23-6" href="#__codelineno-23-6"></a><span class="k">fn</span> <span class="nf">algorithm_B</span><span class="p">(</span><span class="n">n</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-23-7" name="__codelineno-23-7" href="#__codelineno-23-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="n">n</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-23-8" name="__codelineno-23-8" href="#__codelineno-23-8"></a><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-23-9" name="__codelineno-23-9" href="#__codelineno-23-9"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-23-10" name="__codelineno-23-10" href="#__codelineno-23-10"></a><span class="p">}</span>
|
||||||
|
<a id="__codelineno-23-11" name="__codelineno-23-11" href="#__codelineno-23-11"></a><span class="c1">// 算法 C 时间复杂度:常数阶</span>
|
||||||
|
<a id="__codelineno-23-12" name="__codelineno-23-12" href="#__codelineno-23-12"></a><span class="k">fn</span> <span class="nf">algorithm_C</span><span class="p">(</span><span class="n">n</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-23-13" name="__codelineno-23-13" href="#__codelineno-23-13"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="mi">1000000</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-23-14" name="__codelineno-23-14" href="#__codelineno-23-14"></a><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-23-15" name="__codelineno-23-15" href="#__codelineno-23-15"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-23-16" name="__codelineno-23-16" href="#__codelineno-23-16"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<p>算法 <code>A</code> 只有 <span class="arithmatex">\(1\)</span> 个打印操作,算法运行时间不随着 <span class="arithmatex">\(n\)</span> 增大而增长。我们称此算法的时间复杂度为「常数阶」。</p>
|
||||||
|
<p>算法 <code>B</code> 中的打印操作需要循环 <span class="arithmatex">\(n\)</span> 次,算法运行时间随着 <span class="arithmatex">\(n\)</span> 增大呈线性增长。此算法的时间复杂度被称为「线性阶」。</p>
|
||||||
|
<p>算法 <code>C</code> 中的打印操作需要循环 <span class="arithmatex">\(1000000\)</span> 次,虽然运行时间很长,但它与输入数据大小 <span class="arithmatex">\(n\)</span> 无关。因此 <code>C</code> 的时间复杂度和 <code>A</code> 相同,仍为「常数阶」。</p>
|
||||||
<p><img alt="算法 A, B, C 的时间增长趋势" src="../time_complexity.assets/time_complexity_simple_example.png" /></p>
|
<p><img alt="算法 A, B, C 的时间增长趋势" src="../time_complexity.assets/time_complexity_simple_example.png" /></p>
|
||||||
<p align="center"> Fig. 算法 A, B, C 的时间增长趋势 </p>
|
<p align="center"> Fig. 算法 A, B, C 的时间增长趋势 </p>
|
||||||
|
|
||||||
<p>相较于直接统计算法运行时间,时间复杂度分析有哪些优势和局限性呢?</p>
|
<p>相较于直接统计算法运行时间,时间复杂度分析有哪些特点呢?</p>
|
||||||
<p><strong>时间复杂度能够有效评估算法效率</strong>。例如,算法 <code>B</code> 的运行时间呈线性增长,在 <span class="arithmatex">\(n > 1\)</span> 时比算法 <code>A</code> 慢,在 <span class="arithmatex">\(n > 1000000\)</span> 时比算法 <code>C</code> 慢。事实上,只要输入数据大小 <span class="arithmatex">\(n\)</span> 足够大,复杂度为「常数阶」的算法一定优于「线性阶」的算法,这正是时间增长趋势所表达的含义。</p>
|
<p><strong>时间复杂度能够有效评估算法效率</strong>。例如,算法 <code>B</code> 的运行时间呈线性增长,在 <span class="arithmatex">\(n > 1\)</span> 时比算法 <code>A</code> 更慢,在 <span class="arithmatex">\(n > 1000000\)</span> 时比算法 <code>C</code> 更慢。事实上,只要输入数据大小 <span class="arithmatex">\(n\)</span> 足够大,复杂度为“常数阶”的算法一定优于“线性阶”的算法,这正是时间增长趋势所表达的含义。</p>
|
||||||
<p><strong>时间复杂度的推算方法更简便</strong>。显然,运行平台和计算操作类型都与算法运行时间的增长趋势无关。因此在时间复杂度分析中,我们可以简单地将所有计算操作的执行时间视为相同的“单位时间”,从而将“计算操作的运行时间的统计”简化为“计算操作的数量的统计”,这样的简化方法大大降低了估算难度。</p>
|
<p><strong>时间复杂度的推算方法更简便</strong>。显然,运行平台和计算操作类型都与算法运行时间的增长趋势无关。因此在时间复杂度分析中,我们可以简单地将所有计算操作的执行时间视为相同的“单位时间”,从而将“计算操作的运行时间的统计”简化为“计算操作的数量的统计”,这样以来估算难度就大大降低了。</p>
|
||||||
<p><strong>时间复杂度也存在一定的局限性</strong>。例如,尽管算法 <code>A</code> 和 <code>C</code> 的时间复杂度相同,但实际运行时间差别很大。同样,尽管算法 <code>B</code> 的时间复杂度比 <code>C</code> 高,但在输入数据大小 <span class="arithmatex">\(n\)</span> 较小时,算法 <code>B</code> 明显优于算法 <code>C</code> 。在这些情况下,我们很难仅凭时间复杂度判断算法效率高低。当然,尽管存在上述问题,复杂度分析仍然是评判算法效率最有效且常用的方法。</p>
|
<p><strong>时间复杂度也存在一定的局限性</strong>。例如,尽管算法 <code>A</code> 和 <code>C</code> 的时间复杂度相同,但实际运行时间差别很大。同样,尽管算法 <code>B</code> 的时间复杂度比 <code>C</code> 高,但在输入数据大小 <span class="arithmatex">\(n\)</span> 较小时,算法 <code>B</code> 明显优于算法 <code>C</code> 。在这些情况下,我们很难仅凭时间复杂度判断算法效率高低。当然,尽管存在上述问题,复杂度分析仍然是评判算法效率最有效且常用的方法。</p>
|
||||||
<h2 id="223">2.2.3. 函数渐近上界<a class="headerlink" href="#223" title="Permanent link">¶</a></h2>
|
<h2 id="222">2.2.2. 函数渐近上界<a class="headerlink" href="#222" title="Permanent link">¶</a></h2>
|
||||||
<p>设算法的计算操作数量是一个关于输入数据大小 <span class="arithmatex">\(n\)</span> 的函数,记为 <span class="arithmatex">\(T(n)\)</span> ,则以下算法的操作数量为</p>
|
<p>给定一个函数 <code>algorithm()</code> :</p>
|
||||||
<div class="arithmatex">\[
|
|
||||||
T(n) = 3 + 2n
|
|
||||||
\]</div>
|
|
||||||
<div class="tabbed-set tabbed-alternate" data-tabs="3:12"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><input id="__tabbed_3_3" name="__tabbed_3" type="radio" /><input id="__tabbed_3_4" name="__tabbed_3" type="radio" /><input id="__tabbed_3_5" name="__tabbed_3" type="radio" /><input id="__tabbed_3_6" name="__tabbed_3" type="radio" /><input id="__tabbed_3_7" name="__tabbed_3" type="radio" /><input id="__tabbed_3_8" name="__tabbed_3" type="radio" /><input id="__tabbed_3_9" name="__tabbed_3" type="radio" /><input id="__tabbed_3_10" name="__tabbed_3" type="radio" /><input id="__tabbed_3_11" name="__tabbed_3" type="radio" /><input id="__tabbed_3_12" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1">Java</label><label for="__tabbed_3_2">C++</label><label for="__tabbed_3_3">Python</label><label for="__tabbed_3_4">Go</label><label for="__tabbed_3_5">JS</label><label for="__tabbed_3_6">TS</label><label for="__tabbed_3_7">C</label><label for="__tabbed_3_8">C#</label><label for="__tabbed_3_9">Swift</label><label for="__tabbed_3_10">Zig</label><label for="__tabbed_3_11">Dart</label><label for="__tabbed_3_12">Rust</label></div>
|
<div class="tabbed-set tabbed-alternate" data-tabs="3:12"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><input id="__tabbed_3_3" name="__tabbed_3" type="radio" /><input id="__tabbed_3_4" name="__tabbed_3" type="radio" /><input id="__tabbed_3_5" name="__tabbed_3" type="radio" /><input id="__tabbed_3_6" name="__tabbed_3" type="radio" /><input id="__tabbed_3_7" name="__tabbed_3" type="radio" /><input id="__tabbed_3_8" name="__tabbed_3" type="radio" /><input id="__tabbed_3_9" name="__tabbed_3" type="radio" /><input id="__tabbed_3_10" name="__tabbed_3" type="radio" /><input id="__tabbed_3_11" name="__tabbed_3" type="radio" /><input id="__tabbed_3_12" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1">Java</label><label for="__tabbed_3_2">C++</label><label for="__tabbed_3_3">Python</label><label for="__tabbed_3_4">Go</label><label for="__tabbed_3_5">JS</label><label for="__tabbed_3_6">TS</label><label for="__tabbed_3_7">C</label><label for="__tabbed_3_8">C#</label><label for="__tabbed_3_9">Swift</label><label for="__tabbed_3_10">Zig</label><label for="__tabbed_3_11">Dart</label><label for="__tabbed_3_12">Rust</label></div>
|
||||||
<div class="tabbed-content">
|
<div class="tabbed-content">
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -4114,14 +4118,27 @@ T(n) = 3 + 2n
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><pre><span></span><code><a id="__codelineno-35-1" name="__codelineno-35-1" href="#__codelineno-35-1"></a>
|
<div class="highlight"><pre><span></span><code><a id="__codelineno-35-1" name="__codelineno-35-1" href="#__codelineno-35-1"></a><span class="k">fn</span> <span class="nf">algorithm</span><span class="p">(</span><span class="n">n</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-35-2" name="__codelineno-35-2" href="#__codelineno-35-2"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">a</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="c1">// +1</span>
|
||||||
|
<a id="__codelineno-35-3" name="__codelineno-35-3" href="#__codelineno-35-3"></a><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</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="c1">// +1</span>
|
||||||
|
<a id="__codelineno-35-4" name="__codelineno-35-4" href="#__codelineno-35-4"></a><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// +1</span>
|
||||||
|
<a id="__codelineno-35-5" name="__codelineno-35-5" href="#__codelineno-35-5"></a>
|
||||||
|
<a id="__codelineno-35-6" name="__codelineno-35-6" href="#__codelineno-35-6"></a><span class="w"> </span><span class="c1">// 循环 n 次</span>
|
||||||
|
<a id="__codelineno-35-7" name="__codelineno-35-7" href="#__codelineno-35-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="n">n</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// +1(每轮都执行 i ++)</span>
|
||||||
|
<a id="__codelineno-35-8" name="__codelineno-35-8" href="#__codelineno-35-8"></a><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span><span class="w"> </span><span class="c1">// +1</span>
|
||||||
|
<a id="__codelineno-35-9" name="__codelineno-35-9" href="#__codelineno-35-9"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-35-10" name="__codelineno-35-10" href="#__codelineno-35-10"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p><span class="arithmatex">\(T(n)\)</span> 是一次函数,说明时间增长趋势是线性的,因此可以得出时间复杂度是线性阶。</p>
|
<p>设算法的计算操作数量是一个关于输入数据大小 <span class="arithmatex">\(n\)</span> 的函数,记为 <span class="arithmatex">\(T(n)\)</span> ,则以上函数的的操作数量为:</p>
|
||||||
|
<div class="arithmatex">\[
|
||||||
|
T(n) = 3 + 2n
|
||||||
|
\]</div>
|
||||||
|
<p><span class="arithmatex">\(T(n)\)</span> 是一次函数,说明时间的增长趋势是线性的,因此其时间复杂度是线性阶。</p>
|
||||||
<p>我们将线性阶的时间复杂度记为 <span class="arithmatex">\(O(n)\)</span> ,这个数学符号称为「大 <span class="arithmatex">\(O\)</span> 记号 Big-<span class="arithmatex">\(O\)</span> Notation」,表示函数 <span class="arithmatex">\(T(n)\)</span> 的「渐近上界 Asymptotic Upper Bound」。</p>
|
<p>我们将线性阶的时间复杂度记为 <span class="arithmatex">\(O(n)\)</span> ,这个数学符号称为「大 <span class="arithmatex">\(O\)</span> 记号 Big-<span class="arithmatex">\(O\)</span> Notation」,表示函数 <span class="arithmatex">\(T(n)\)</span> 的「渐近上界 Asymptotic Upper Bound」。</p>
|
||||||
<p>推算时间复杂度本质上是计算“操作数量函数 <span class="arithmatex">\(T(n)\)</span>”的渐近上界。接下来,我们来看函数渐近上界的数学定义。</p>
|
<p>时间复杂度分析本质上是计算“操作数量函数 <span class="arithmatex">\(T(n)\)</span>”的渐近上界。接下来,我们来看函数渐近上界的数学定义。</p>
|
||||||
<div class="admonition abstract">
|
<div class="admonition abstract">
|
||||||
<p class="admonition-title">函数渐近上界</p>
|
<p class="admonition-title">函数渐近上界</p>
|
||||||
<p>若存在正实数 <span class="arithmatex">\(c\)</span> 和实数 <span class="arithmatex">\(n_0\)</span> ,使得对于所有的 <span class="arithmatex">\(n > n_0\)</span> ,均有
|
<p>若存在正实数 <span class="arithmatex">\(c\)</span> 和实数 <span class="arithmatex">\(n_0\)</span> ,使得对于所有的 <span class="arithmatex">\(n > n_0\)</span> ,均有
|
||||||
|
@ -4136,18 +4153,18 @@ $$</p>
|
||||||
<p><img alt="函数的渐近上界" src="../time_complexity.assets/asymptotic_upper_bound.png" /></p>
|
<p><img alt="函数的渐近上界" src="../time_complexity.assets/asymptotic_upper_bound.png" /></p>
|
||||||
<p align="center"> Fig. 函数的渐近上界 </p>
|
<p align="center"> Fig. 函数的渐近上界 </p>
|
||||||
|
|
||||||
<p>从本质上讲,计算渐近上界就是寻找一个函数 <span class="arithmatex">\(f(n)\)</span> ,使得当 <span class="arithmatex">\(n\)</span> 趋向于无穷大时,<span class="arithmatex">\(T(n)\)</span> 和 <span class="arithmatex">\(f(n)\)</span> 处于相同的增长级别,仅相差一个常数项 <span class="arithmatex">\(c\)</span> 的倍数。</p>
|
<p>也就是说,计算渐近上界就是寻找一个函数 <span class="arithmatex">\(f(n)\)</span> ,使得当 <span class="arithmatex">\(n\)</span> 趋向于无穷大时,<span class="arithmatex">\(T(n)\)</span> 和 <span class="arithmatex">\(f(n)\)</span> 处于相同的增长级别,仅相差一个常数项 <span class="arithmatex">\(c\)</span> 的倍数。</p>
|
||||||
<h2 id="224">2.2.4. 推算方法<a class="headerlink" href="#224" title="Permanent link">¶</a></h2>
|
<h2 id="223">2.2.3. 推算方法<a class="headerlink" href="#223" title="Permanent link">¶</a></h2>
|
||||||
<p>渐近上界的数学味儿有点重,如果你感觉没有完全理解,也无需担心。因为在实际使用中,我们只需要掌握推算方法,数学意义可以逐渐领悟。</p>
|
<p>渐近上界的数学味儿有点重,如果你感觉没有完全理解,也无需担心。因为在实际使用中,我们只需要掌握推算方法,数学意义可以逐渐领悟。</p>
|
||||||
<p>根据定义,确定 <span class="arithmatex">\(f(n)\)</span> 之后,我们便可得到时间复杂度 <span class="arithmatex">\(O(f(n))\)</span> 。那么如何确定渐近上界 <span class="arithmatex">\(f(n)\)</span> 呢?总体分为两步:首先统计操作数量,然后判断渐近上界。</p>
|
<p>根据定义,确定 <span class="arithmatex">\(f(n)\)</span> 之后,我们便可得到时间复杂度 <span class="arithmatex">\(O(f(n))\)</span> 。那么如何确定渐近上界 <span class="arithmatex">\(f(n)\)</span> 呢?总体分为两步:首先统计操作数量,然后判断渐近上界。</p>
|
||||||
<h3 id="_1">第一步:统计操作数量<a class="headerlink" href="#_1" title="Permanent link">¶</a></h3>
|
<h3 id="_1">第一步:统计操作数量<a class="headerlink" href="#_1" title="Permanent link">¶</a></h3>
|
||||||
<p>针对代码,逐行从上到下计算即可。然而,由于上述 <span class="arithmatex">\(c \cdot f(n)\)</span> 中的常数项 <span class="arithmatex">\(c\)</span> 可以取任意大小,<strong>因此操作数量 <span class="arithmatex">\(T(n)\)</span> 中的各种系数、常数项都可以被忽略</strong>。根据此原则,可以总结出以下计数简化技巧:</p>
|
<p>针对代码,逐行从上到下计算即可。然而,由于上述 <span class="arithmatex">\(c \cdot f(n)\)</span> 中的常数项 <span class="arithmatex">\(c\)</span> 可以取任意大小,<strong>因此操作数量 <span class="arithmatex">\(T(n)\)</span> 中的各种系数、常数项都可以被忽略</strong>。根据此原则,可以总结出以下计数简化技巧:</p>
|
||||||
<ol>
|
<ol>
|
||||||
<li><strong>忽略与 <span class="arithmatex">\(n\)</span> 无关的操作</strong>。因为它们都是 <span class="arithmatex">\(T(n)\)</span> 中的常数项,对时间复杂度不产生影响。</li>
|
<li><strong>忽略 <span class="arithmatex">\(T(n)\)</span> 中的常数项</strong>。因为它们都与 <span class="arithmatex">\(n\)</span> 无关,所以对时间复杂度不产生影响。</li>
|
||||||
<li><strong>省略所有系数</strong>。例如,循环 <span class="arithmatex">\(2n\)</span> 次、<span class="arithmatex">\(5n + 1\)</span> 次等,都可以简化记为 <span class="arithmatex">\(n\)</span> 次,因为 <span class="arithmatex">\(n\)</span> 前面的系数对时间复杂度没有影响。</li>
|
<li><strong>省略所有系数</strong>。例如,循环 <span class="arithmatex">\(2n\)</span> 次、<span class="arithmatex">\(5n + 1\)</span> 次等,都可以简化记为 <span class="arithmatex">\(n\)</span> 次,因为 <span class="arithmatex">\(n\)</span> 前面的系数对时间复杂度没有影响。</li>
|
||||||
<li><strong>循环嵌套时使用乘法</strong>。总操作数量等于外层循环和内层循环操作数量之积,每一层循环依然可以分别套用上述 <code>1.</code> 和 <code>2.</code> 技巧。</li>
|
<li><strong>循环嵌套时使用乘法</strong>。总操作数量等于外层循环和内层循环操作数量之积,每一层循环依然可以分别套用上述 <code>1.</code> 和 <code>2.</code> 技巧。</li>
|
||||||
</ol>
|
</ol>
|
||||||
<p>以下示例展示了使用上述技巧前、后的统计结果。</p>
|
<p>以下示例展示了使用上述技巧前、后的统计结果。两者推出的时间复杂度相同,即为 <span class="arithmatex">\(O(n^2)\)</span> 。</p>
|
||||||
<div class="arithmatex">\[
|
<div class="arithmatex">\[
|
||||||
\begin{aligned}
|
\begin{aligned}
|
||||||
T(n) & = 2n(n + 1) + (5n + 1) + 2 & \text{完整统计 (-.-|||)} \newline
|
T(n) & = 2n(n + 1) + (5n + 1) + 2 & \text{完整统计 (-.-|||)} \newline
|
||||||
|
@ -4155,7 +4172,6 @@ T(n) & = 2n(n + 1) + (5n + 1) + 2 & \text{完整统计 (-.-|||)} \newlin
|
||||||
T(n) & = n^2 + n & \text{偷懒统计 (o.O)}
|
T(n) & = n^2 + n & \text{偷懒统计 (o.O)}
|
||||||
\end{aligned}
|
\end{aligned}
|
||||||
\]</div>
|
\]</div>
|
||||||
<p>最终,两者都能推出相同的时间复杂度结果,即 <span class="arithmatex">\(O(n^2)\)</span> 。</p>
|
|
||||||
<div class="tabbed-set tabbed-alternate" data-tabs="4:12"><input checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio" /><input id="__tabbed_4_2" name="__tabbed_4" type="radio" /><input id="__tabbed_4_3" name="__tabbed_4" type="radio" /><input id="__tabbed_4_4" name="__tabbed_4" type="radio" /><input id="__tabbed_4_5" name="__tabbed_4" type="radio" /><input id="__tabbed_4_6" name="__tabbed_4" type="radio" /><input id="__tabbed_4_7" name="__tabbed_4" type="radio" /><input id="__tabbed_4_8" name="__tabbed_4" type="radio" /><input id="__tabbed_4_9" name="__tabbed_4" type="radio" /><input id="__tabbed_4_10" name="__tabbed_4" type="radio" /><input id="__tabbed_4_11" name="__tabbed_4" type="radio" /><input id="__tabbed_4_12" name="__tabbed_4" type="radio" /><div class="tabbed-labels"><label for="__tabbed_4_1">Java</label><label for="__tabbed_4_2">C++</label><label for="__tabbed_4_3">Python</label><label for="__tabbed_4_4">Go</label><label for="__tabbed_4_5">JS</label><label for="__tabbed_4_6">TS</label><label for="__tabbed_4_7">C</label><label for="__tabbed_4_8">C#</label><label for="__tabbed_4_9">Swift</label><label for="__tabbed_4_10">Zig</label><label for="__tabbed_4_11">Dart</label><label for="__tabbed_4_12">Rust</label></div>
|
<div class="tabbed-set tabbed-alternate" data-tabs="4:12"><input checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio" /><input id="__tabbed_4_2" name="__tabbed_4" type="radio" /><input id="__tabbed_4_3" name="__tabbed_4" type="radio" /><input id="__tabbed_4_4" name="__tabbed_4" type="radio" /><input id="__tabbed_4_5" name="__tabbed_4" type="radio" /><input id="__tabbed_4_6" name="__tabbed_4" type="radio" /><input id="__tabbed_4_7" name="__tabbed_4" type="radio" /><input id="__tabbed_4_8" name="__tabbed_4" type="radio" /><input id="__tabbed_4_9" name="__tabbed_4" type="radio" /><input id="__tabbed_4_10" name="__tabbed_4" type="radio" /><input id="__tabbed_4_11" name="__tabbed_4" type="radio" /><input id="__tabbed_4_12" name="__tabbed_4" type="radio" /><div class="tabbed-labels"><label for="__tabbed_4_1">Java</label><label for="__tabbed_4_2">C++</label><label for="__tabbed_4_3">Python</label><label for="__tabbed_4_4">Go</label><label for="__tabbed_4_5">JS</label><label for="__tabbed_4_6">TS</label><label for="__tabbed_4_7">C</label><label for="__tabbed_4_8">C#</label><label for="__tabbed_4_9">Swift</label><label for="__tabbed_4_10">Zig</label><label for="__tabbed_4_11">Dart</label><label for="__tabbed_4_12">Rust</label></div>
|
||||||
<div class="tabbed-content">
|
<div class="tabbed-content">
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -4329,7 +4345,22 @@ T(n) & = n^2 + n & \text{偷懒统计 (o.O)}
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><pre><span></span><code><a id="__codelineno-47-1" name="__codelineno-47-1" href="#__codelineno-47-1"></a>
|
<div class="highlight"><pre><span></span><code><a id="__codelineno-47-1" name="__codelineno-47-1" href="#__codelineno-47-1"></a><span class="k">fn</span> <span class="nf">algorithm</span><span class="p">(</span><span class="n">n</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-47-2" name="__codelineno-47-2" href="#__codelineno-47-2"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">a</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="c1">// +0(技巧 1)</span>
|
||||||
|
<a id="__codelineno-47-3" name="__codelineno-47-3" href="#__codelineno-47-3"></a><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="c1">// +0(技巧 1)</span>
|
||||||
|
<a id="__codelineno-47-4" name="__codelineno-47-4" href="#__codelineno-47-4"></a>
|
||||||
|
<a id="__codelineno-47-5" name="__codelineno-47-5" href="#__codelineno-47-5"></a><span class="w"> </span><span class="c1">// +n(技巧 2)</span>
|
||||||
|
<a id="__codelineno-47-6" name="__codelineno-47-6" href="#__codelineno-47-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="p">(</span><span class="mi">5</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">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="p">{</span>
|
||||||
|
<a id="__codelineno-47-7" name="__codelineno-47-7" href="#__codelineno-47-7"></a><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-47-8" name="__codelineno-47-8" href="#__codelineno-47-8"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-47-9" name="__codelineno-47-9" href="#__codelineno-47-9"></a>
|
||||||
|
<a id="__codelineno-47-10" name="__codelineno-47-10" href="#__codelineno-47-10"></a><span class="w"> </span><span class="c1">// +n*n(技巧 3)</span>
|
||||||
|
<a id="__codelineno-47-11" name="__codelineno-47-11" href="#__codelineno-47-11"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="p">(</span><span class="mi">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
|
<a id="__codelineno-47-12" name="__codelineno-47-12" href="#__codelineno-47-12"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="p">(</span><span class="n">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="p">{</span>
|
||||||
|
<a id="__codelineno-47-13" name="__codelineno-47-13" href="#__codelineno-47-13"></a><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
|
||||||
|
<a id="__codelineno-47-14" name="__codelineno-47-14" href="#__codelineno-47-14"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-47-15" name="__codelineno-47-15" href="#__codelineno-47-15"></a><span class="w"> </span><span class="p">}</span>
|
||||||
|
<a id="__codelineno-47-16" name="__codelineno-47-16" href="#__codelineno-47-16"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -4369,7 +4400,7 @@ T(n) & = n^2 + n & \text{偷懒统计 (o.O)}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
<h2 id="225">2.2.5. 常见类型<a class="headerlink" href="#225" title="Permanent link">¶</a></h2>
|
<h2 id="224">2.2.4. 常见类型<a class="headerlink" href="#224" title="Permanent link">¶</a></h2>
|
||||||
<p>设输入数据大小为 <span class="arithmatex">\(n\)</span> ,常见的时间复杂度类型包括(按照从低到高的顺序排列):</p>
|
<p>设输入数据大小为 <span class="arithmatex">\(n\)</span> ,常见的时间复杂度类型包括(按照从低到高的顺序排列):</p>
|
||||||
<div class="arithmatex">\[
|
<div class="arithmatex">\[
|
||||||
\begin{aligned}
|
\begin{aligned}
|
||||||
|
@ -4382,7 +4413,7 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!
|
||||||
|
|
||||||
<div class="admonition tip">
|
<div class="admonition tip">
|
||||||
<p class="admonition-title">Tip</p>
|
<p class="admonition-title">Tip</p>
|
||||||
<p>部分示例代码需要一些预备知识,包括数组、递归算法等。如果遇到不理解的部分,请不要担心,可以在学习完后面章节后再回顾。现阶段,请先专注于理解时间复杂度的含义和推算方法。</p>
|
<p>部分示例代码需要一些预备知识,包括数组、递归等。如果你遇到不理解的部分,可以在学习完后面章节后再回顾。现阶段,请先专注于理解时间复杂度的含义和推算方法。</p>
|
||||||
</div>
|
</div>
|
||||||
<h3 id="o1">常数阶 <span class="arithmatex">\(O(1)\)</span><a class="headerlink" href="#o1" title="Permanent link">¶</a></h3>
|
<h3 id="o1">常数阶 <span class="arithmatex">\(O(1)\)</span><a class="headerlink" href="#o1" title="Permanent link">¶</a></h3>
|
||||||
<p>常数阶的操作数量与输入数据大小 <span class="arithmatex">\(n\)</span> 无关,即不随着 <span class="arithmatex">\(n\)</span> 的变化而变化。</p>
|
<p>常数阶的操作数量与输入数据大小 <span class="arithmatex">\(n\)</span> 无关,即不随着 <span class="arithmatex">\(n\)</span> 的变化而变化。</p>
|
||||||
|
@ -4661,10 +4692,6 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p>遍历数组和遍历链表等操作的时间复杂度均为 <span class="arithmatex">\(O(n)\)</span> ,其中 <span class="arithmatex">\(n\)</span> 为数组或链表的长度。</p>
|
<p>遍历数组和遍历链表等操作的时间复杂度均为 <span class="arithmatex">\(O(n)\)</span> ,其中 <span class="arithmatex">\(n\)</span> 为数组或链表的长度。</p>
|
||||||
<div class="admonition question">
|
|
||||||
<p class="admonition-title">如何确定输入数据大小 <span class="arithmatex">\(n\)</span> ?</p>
|
|
||||||
<p><strong>数据大小 <span class="arithmatex">\(n\)</span> 需根据输入数据的类型来具体确定</strong>。例如,在上述示例中,我们直接将 <span class="arithmatex">\(n\)</span> 视为输入数据大小;在下面遍历数组的示例中,数据大小 <span class="arithmatex">\(n\)</span> 为数组的长度。</p>
|
|
||||||
</div>
|
|
||||||
<div class="tabbed-set tabbed-alternate" data-tabs="7:12"><input checked="checked" id="__tabbed_7_1" name="__tabbed_7" type="radio" /><input id="__tabbed_7_2" name="__tabbed_7" type="radio" /><input id="__tabbed_7_3" name="__tabbed_7" type="radio" /><input id="__tabbed_7_4" name="__tabbed_7" type="radio" /><input id="__tabbed_7_5" name="__tabbed_7" type="radio" /><input id="__tabbed_7_6" name="__tabbed_7" type="radio" /><input id="__tabbed_7_7" name="__tabbed_7" type="radio" /><input id="__tabbed_7_8" name="__tabbed_7" type="radio" /><input id="__tabbed_7_9" name="__tabbed_7" type="radio" /><input id="__tabbed_7_10" name="__tabbed_7" type="radio" /><input id="__tabbed_7_11" name="__tabbed_7" type="radio" /><input id="__tabbed_7_12" name="__tabbed_7" type="radio" /><div class="tabbed-labels"><label for="__tabbed_7_1">Java</label><label for="__tabbed_7_2">C++</label><label for="__tabbed_7_3">Python</label><label for="__tabbed_7_4">Go</label><label for="__tabbed_7_5">JS</label><label for="__tabbed_7_6">TS</label><label for="__tabbed_7_7">C</label><label for="__tabbed_7_8">C#</label><label for="__tabbed_7_9">Swift</label><label for="__tabbed_7_10">Zig</label><label for="__tabbed_7_11">Dart</label><label for="__tabbed_7_12">Rust</label></div>
|
<div class="tabbed-set tabbed-alternate" data-tabs="7:12"><input checked="checked" id="__tabbed_7_1" name="__tabbed_7" type="radio" /><input id="__tabbed_7_2" name="__tabbed_7" type="radio" /><input id="__tabbed_7_3" name="__tabbed_7" type="radio" /><input id="__tabbed_7_4" name="__tabbed_7" type="radio" /><input id="__tabbed_7_5" name="__tabbed_7" type="radio" /><input id="__tabbed_7_6" name="__tabbed_7" type="radio" /><input id="__tabbed_7_7" name="__tabbed_7" type="radio" /><input id="__tabbed_7_8" name="__tabbed_7" type="radio" /><input id="__tabbed_7_9" name="__tabbed_7" type="radio" /><input id="__tabbed_7_10" name="__tabbed_7" type="radio" /><input id="__tabbed_7_11" name="__tabbed_7" type="radio" /><input id="__tabbed_7_12" name="__tabbed_7" type="radio" /><div class="tabbed-labels"><label for="__tabbed_7_1">Java</label><label for="__tabbed_7_2">C++</label><label for="__tabbed_7_3">Python</label><label for="__tabbed_7_4">Go</label><label for="__tabbed_7_5">JS</label><label for="__tabbed_7_6">TS</label><label for="__tabbed_7_7">C</label><label for="__tabbed_7_8">C#</label><label for="__tabbed_7_9">Swift</label><label for="__tabbed_7_10">Zig</label><label for="__tabbed_7_11">Dart</label><label for="__tabbed_7_12">Rust</label></div>
|
||||||
<div class="tabbed-content">
|
<div class="tabbed-content">
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -4811,6 +4838,7 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<p>值得注意的是,<strong>数据大小 <span class="arithmatex">\(n\)</span> 需根据输入数据的类型来具体确定</strong>。比如在第一个示例中,变量 <span class="arithmatex">\(n\)</span> 为输入数据大小;在第二个示例中,数组长度 <span class="arithmatex">\(n\)</span> 为数据大小。</p>
|
||||||
<h3 id="on2">平方阶 <span class="arithmatex">\(O(n^2)\)</span><a class="headerlink" href="#on2" title="Permanent link">¶</a></h3>
|
<h3 id="on2">平方阶 <span class="arithmatex">\(O(n^2)\)</span><a class="headerlink" href="#on2" title="Permanent link">¶</a></h3>
|
||||||
<p>平方阶的操作数量相对于输入数据大小以平方级别增长。平方阶通常出现在嵌套循环中,外层循环和内层循环都为 <span class="arithmatex">\(O(n)\)</span> ,因此总体为 <span class="arithmatex">\(O(n^2)\)</span> 。</p>
|
<p>平方阶的操作数量相对于输入数据大小以平方级别增长。平方阶通常出现在嵌套循环中,外层循环和内层循环都为 <span class="arithmatex">\(O(n)\)</span> ,因此总体为 <span class="arithmatex">\(O(n^2)\)</span> 。</p>
|
||||||
<div class="tabbed-set tabbed-alternate" data-tabs="8:12"><input checked="checked" id="__tabbed_8_1" name="__tabbed_8" type="radio" /><input id="__tabbed_8_2" name="__tabbed_8" type="radio" /><input id="__tabbed_8_3" name="__tabbed_8" type="radio" /><input id="__tabbed_8_4" name="__tabbed_8" type="radio" /><input id="__tabbed_8_5" name="__tabbed_8" type="radio" /><input id="__tabbed_8_6" name="__tabbed_8" type="radio" /><input id="__tabbed_8_7" name="__tabbed_8" type="radio" /><input id="__tabbed_8_8" name="__tabbed_8" type="radio" /><input id="__tabbed_8_9" name="__tabbed_8" type="radio" /><input id="__tabbed_8_10" name="__tabbed_8" type="radio" /><input id="__tabbed_8_11" name="__tabbed_8" type="radio" /><input id="__tabbed_8_12" name="__tabbed_8" type="radio" /><div class="tabbed-labels"><label for="__tabbed_8_1">Java</label><label for="__tabbed_8_2">C++</label><label for="__tabbed_8_3">Python</label><label for="__tabbed_8_4">Go</label><label for="__tabbed_8_5">JS</label><label for="__tabbed_8_6">TS</label><label for="__tabbed_8_7">C</label><label for="__tabbed_8_8">C#</label><label for="__tabbed_8_9">Swift</label><label for="__tabbed_8_10">Zig</label><label for="__tabbed_8_11">Dart</label><label for="__tabbed_8_12">Rust</label></div>
|
<div class="tabbed-set tabbed-alternate" data-tabs="8:12"><input checked="checked" id="__tabbed_8_1" name="__tabbed_8" type="radio" /><input id="__tabbed_8_2" name="__tabbed_8" type="radio" /><input id="__tabbed_8_3" name="__tabbed_8" type="radio" /><input id="__tabbed_8_4" name="__tabbed_8" type="radio" /><input id="__tabbed_8_5" name="__tabbed_8" type="radio" /><input id="__tabbed_8_6" name="__tabbed_8" type="radio" /><input id="__tabbed_8_7" name="__tabbed_8" type="radio" /><input id="__tabbed_8_8" name="__tabbed_8" type="radio" /><input id="__tabbed_8_9" name="__tabbed_8" type="radio" /><input id="__tabbed_8_10" name="__tabbed_8" type="radio" /><input id="__tabbed_8_11" name="__tabbed_8" type="radio" /><input id="__tabbed_8_12" name="__tabbed_8" type="radio" /><div class="tabbed-labels"><label for="__tabbed_8_1">Java</label><label for="__tabbed_8_2">C++</label><label for="__tabbed_8_3">Python</label><label for="__tabbed_8_4">Go</label><label for="__tabbed_8_5">JS</label><label for="__tabbed_8_6">TS</label><label for="__tabbed_8_7">C</label><label for="__tabbed_8_8">C#</label><label for="__tabbed_8_9">Swift</label><label for="__tabbed_8_10">Zig</label><label for="__tabbed_8_11">Dart</label><label for="__tabbed_8_12">Rust</label></div>
|
||||||
|
@ -5244,18 +5272,15 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<h3 id="o2n">指数阶 <span class="arithmatex">\(O(2^n)\)</span><a class="headerlink" href="#o2n" title="Permanent link">¶</a></h3>
|
<h3 id="o2n">指数阶 <span class="arithmatex">\(O(2^n)\)</span><a class="headerlink" href="#o2n" title="Permanent link">¶</a></h3>
|
||||||
<div class="admonition note">
|
|
||||||
<p class="admonition-title">Note</p>
|
|
||||||
<p>生物学的“细胞分裂”是指数阶增长的典型例子:初始状态为 <span class="arithmatex">\(1\)</span> 个细胞,分裂一轮后变为 <span class="arithmatex">\(2\)</span> 个,分裂两轮后变为 <span class="arithmatex">\(4\)</span> 个,以此类推,分裂 <span class="arithmatex">\(n\)</span> 轮后有 <span class="arithmatex">\(2^n\)</span> 个细胞。</p>
|
<p>生物学的“细胞分裂”是指数阶增长的典型例子:初始状态为 <span class="arithmatex">\(1\)</span> 个细胞,分裂一轮后变为 <span class="arithmatex">\(2\)</span> 个,分裂两轮后变为 <span class="arithmatex">\(4\)</span> 个,以此类推,分裂 <span class="arithmatex">\(n\)</span> 轮后有 <span class="arithmatex">\(2^n\)</span> 个细胞。</p>
|
||||||
</div>
|
<p>以下代码模拟了细胞分裂的过程。</p>
|
||||||
<p>指数阶增长非常迅速,在实际应用中通常是不可接受的。若一个问题使用「暴力枚举」求解的时间复杂度为 <span class="arithmatex">\(O(2^n)\)</span> ,那么通常需要使用「动态规划」或「贪心算法」等方法来解决。</p>
|
|
||||||
<div class="tabbed-set tabbed-alternate" data-tabs="10:12"><input checked="checked" id="__tabbed_10_1" name="__tabbed_10" type="radio" /><input id="__tabbed_10_2" name="__tabbed_10" type="radio" /><input id="__tabbed_10_3" name="__tabbed_10" type="radio" /><input id="__tabbed_10_4" name="__tabbed_10" type="radio" /><input id="__tabbed_10_5" name="__tabbed_10" type="radio" /><input id="__tabbed_10_6" name="__tabbed_10" type="radio" /><input id="__tabbed_10_7" name="__tabbed_10" type="radio" /><input id="__tabbed_10_8" name="__tabbed_10" type="radio" /><input id="__tabbed_10_9" name="__tabbed_10" type="radio" /><input id="__tabbed_10_10" name="__tabbed_10" type="radio" /><input id="__tabbed_10_11" name="__tabbed_10" type="radio" /><input id="__tabbed_10_12" name="__tabbed_10" type="radio" /><div class="tabbed-labels"><label for="__tabbed_10_1">Java</label><label for="__tabbed_10_2">C++</label><label for="__tabbed_10_3">Python</label><label for="__tabbed_10_4">Go</label><label for="__tabbed_10_5">JS</label><label for="__tabbed_10_6">TS</label><label for="__tabbed_10_7">C</label><label for="__tabbed_10_8">C#</label><label for="__tabbed_10_9">Swift</label><label for="__tabbed_10_10">Zig</label><label for="__tabbed_10_11">Dart</label><label for="__tabbed_10_12">Rust</label></div>
|
<div class="tabbed-set tabbed-alternate" data-tabs="10:12"><input checked="checked" id="__tabbed_10_1" name="__tabbed_10" type="radio" /><input id="__tabbed_10_2" name="__tabbed_10" type="radio" /><input id="__tabbed_10_3" name="__tabbed_10" type="radio" /><input id="__tabbed_10_4" name="__tabbed_10" type="radio" /><input id="__tabbed_10_5" name="__tabbed_10" type="radio" /><input id="__tabbed_10_6" name="__tabbed_10" type="radio" /><input id="__tabbed_10_7" name="__tabbed_10" type="radio" /><input id="__tabbed_10_8" name="__tabbed_10" type="radio" /><input id="__tabbed_10_9" name="__tabbed_10" type="radio" /><input id="__tabbed_10_10" name="__tabbed_10" type="radio" /><input id="__tabbed_10_11" name="__tabbed_10" type="radio" /><input id="__tabbed_10_12" name="__tabbed_10" type="radio" /><div class="tabbed-labels"><label for="__tabbed_10_1">Java</label><label for="__tabbed_10_2">C++</label><label for="__tabbed_10_3">Python</label><label for="__tabbed_10_4">Go</label><label for="__tabbed_10_5">JS</label><label for="__tabbed_10_6">TS</label><label for="__tabbed_10_7">C</label><label for="__tabbed_10_8">C#</label><label for="__tabbed_10_9">Swift</label><label for="__tabbed_10_10">Zig</label><label for="__tabbed_10_11">Dart</label><label for="__tabbed_10_12">Rust</label></div>
|
||||||
<div class="tabbed-content">
|
<div class="tabbed-content">
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">time_complexity.java</span><pre><span></span><code><a id="__codelineno-108-1" name="__codelineno-108-1" href="#__codelineno-108-1"></a><span class="cm">/* 指数阶(循环实现) */</span>
|
<div class="highlight"><span class="filename">time_complexity.java</span><pre><span></span><code><a id="__codelineno-108-1" name="__codelineno-108-1" href="#__codelineno-108-1"></a><span class="cm">/* 指数阶(循环实现) */</span>
|
||||||
<a id="__codelineno-108-2" name="__codelineno-108-2" href="#__codelineno-108-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">exponential</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-108-2" name="__codelineno-108-2" href="#__codelineno-108-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">exponential</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-108-3" name="__codelineno-108-3" href="#__codelineno-108-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</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="n">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
|
<a id="__codelineno-108-3" name="__codelineno-108-3" href="#__codelineno-108-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</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="n">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
|
||||||
<a id="__codelineno-108-4" name="__codelineno-108-4" href="#__codelineno-108-4"></a><span class="w"> </span><span class="c1">// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
<a id="__codelineno-108-4" name="__codelineno-108-4" href="#__codelineno-108-4"></a><span class="w"> </span><span class="c1">// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
||||||
<a id="__codelineno-108-5" name="__codelineno-108-5" href="#__codelineno-108-5"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-108-5" name="__codelineno-108-5" href="#__codelineno-108-5"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-108-6" name="__codelineno-108-6" href="#__codelineno-108-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">j</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="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">base</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-108-6" name="__codelineno-108-6" href="#__codelineno-108-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">j</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="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">base</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-108-7" name="__codelineno-108-7" href="#__codelineno-108-7"></a><span class="w"> </span><span class="n">count</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-108-7" name="__codelineno-108-7" href="#__codelineno-108-7"></a><span class="w"> </span><span class="n">count</span><span class="o">++</span><span class="p">;</span>
|
||||||
|
@ -5271,7 +5296,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
<div class="highlight"><span class="filename">time_complexity.cpp</span><pre><span></span><code><a id="__codelineno-109-1" name="__codelineno-109-1" href="#__codelineno-109-1"></a><span class="cm">/* 指数阶(循环实现) */</span>
|
<div class="highlight"><span class="filename">time_complexity.cpp</span><pre><span></span><code><a id="__codelineno-109-1" name="__codelineno-109-1" href="#__codelineno-109-1"></a><span class="cm">/* 指数阶(循环实现) */</span>
|
||||||
<a id="__codelineno-109-2" name="__codelineno-109-2" href="#__codelineno-109-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">exponential</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-109-2" name="__codelineno-109-2" href="#__codelineno-109-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">exponential</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-109-3" name="__codelineno-109-3" href="#__codelineno-109-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</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="n">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
|
<a id="__codelineno-109-3" name="__codelineno-109-3" href="#__codelineno-109-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</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="n">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
|
||||||
<a id="__codelineno-109-4" name="__codelineno-109-4" href="#__codelineno-109-4"></a><span class="w"> </span><span class="c1">// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
<a id="__codelineno-109-4" name="__codelineno-109-4" href="#__codelineno-109-4"></a><span class="w"> </span><span class="c1">// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
||||||
<a id="__codelineno-109-5" name="__codelineno-109-5" href="#__codelineno-109-5"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-109-5" name="__codelineno-109-5" href="#__codelineno-109-5"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-109-6" name="__codelineno-109-6" href="#__codelineno-109-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">j</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="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">base</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-109-6" name="__codelineno-109-6" href="#__codelineno-109-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">j</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="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">base</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-109-7" name="__codelineno-109-7" href="#__codelineno-109-7"></a><span class="w"> </span><span class="n">count</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-109-7" name="__codelineno-109-7" href="#__codelineno-109-7"></a><span class="w"> </span><span class="n">count</span><span class="o">++</span><span class="p">;</span>
|
||||||
|
@ -5288,7 +5313,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
<a id="__codelineno-110-2" name="__codelineno-110-2" href="#__codelineno-110-2"></a><span class="w"> </span><span class="sd">"""指数阶(循环实现)"""</span>
|
<a id="__codelineno-110-2" name="__codelineno-110-2" href="#__codelineno-110-2"></a><span class="w"> </span><span class="sd">"""指数阶(循环实现)"""</span>
|
||||||
<a id="__codelineno-110-3" name="__codelineno-110-3" href="#__codelineno-110-3"></a> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
|
<a id="__codelineno-110-3" name="__codelineno-110-3" href="#__codelineno-110-3"></a> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
|
||||||
<a id="__codelineno-110-4" name="__codelineno-110-4" href="#__codelineno-110-4"></a> <span class="n">base</span> <span class="o">=</span> <span class="mi">1</span>
|
<a id="__codelineno-110-4" name="__codelineno-110-4" href="#__codelineno-110-4"></a> <span class="n">base</span> <span class="o">=</span> <span class="mi">1</span>
|
||||||
<a id="__codelineno-110-5" name="__codelineno-110-5" href="#__codelineno-110-5"></a> <span class="c1"># cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
<a id="__codelineno-110-5" name="__codelineno-110-5" href="#__codelineno-110-5"></a> <span class="c1"># 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
||||||
<a id="__codelineno-110-6" name="__codelineno-110-6" href="#__codelineno-110-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-110-6" name="__codelineno-110-6" href="#__codelineno-110-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-110-7" name="__codelineno-110-7" href="#__codelineno-110-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>
|
<a id="__codelineno-110-7" name="__codelineno-110-7" href="#__codelineno-110-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>
|
||||||
<a id="__codelineno-110-8" name="__codelineno-110-8" href="#__codelineno-110-8"></a> <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
|
<a id="__codelineno-110-8" name="__codelineno-110-8" href="#__codelineno-110-8"></a> <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
|
||||||
|
@ -5301,7 +5326,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
<div class="highlight"><span class="filename">time_complexity.go</span><pre><span></span><code><a id="__codelineno-111-1" name="__codelineno-111-1" href="#__codelineno-111-1"></a><span class="cm">/* 指数阶(循环实现)*/</span>
|
<div class="highlight"><span class="filename">time_complexity.go</span><pre><span></span><code><a id="__codelineno-111-1" name="__codelineno-111-1" href="#__codelineno-111-1"></a><span class="cm">/* 指数阶(循环实现)*/</span>
|
||||||
<a id="__codelineno-111-2" name="__codelineno-111-2" href="#__codelineno-111-2"></a><span class="kd">func</span><span class="w"> </span><span class="nx">exponential</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="kt">int</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-111-2" name="__codelineno-111-2" href="#__codelineno-111-2"></a><span class="kd">func</span><span class="w"> </span><span class="nx">exponential</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="kt">int</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-111-3" name="__codelineno-111-3" href="#__codelineno-111-3"></a><span class="w"> </span><span class="nx">count</span><span class="p">,</span><span class="w"> </span><span class="nx">base</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="mi">1</span>
|
<a id="__codelineno-111-3" name="__codelineno-111-3" href="#__codelineno-111-3"></a><span class="w"> </span><span class="nx">count</span><span class="p">,</span><span class="w"> </span><span class="nx">base</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="mi">1</span>
|
||||||
<a id="__codelineno-111-4" name="__codelineno-111-4" href="#__codelineno-111-4"></a><span class="w"> </span><span class="c1">// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
<a id="__codelineno-111-4" name="__codelineno-111-4" href="#__codelineno-111-4"></a><span class="w"> </span><span class="c1">// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
||||||
<a id="__codelineno-111-5" name="__codelineno-111-5" href="#__codelineno-111-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="nx">n</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>
|
<a id="__codelineno-111-5" name="__codelineno-111-5" href="#__codelineno-111-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="nx">n</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>
|
||||||
<a id="__codelineno-111-6" name="__codelineno-111-6" href="#__codelineno-111-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nx">j</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">j</span><span class="w"> </span><span class="p"><</span><span class="w"> </span><span class="nx">base</span><span class="p">;</span><span class="w"> </span><span class="nx">j</span><span class="o">++</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-111-6" name="__codelineno-111-6" href="#__codelineno-111-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nx">j</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">j</span><span class="w"> </span><span class="p"><</span><span class="w"> </span><span class="nx">base</span><span class="p">;</span><span class="w"> </span><span class="nx">j</span><span class="o">++</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-111-7" name="__codelineno-111-7" href="#__codelineno-111-7"></a><span class="w"> </span><span class="nx">count</span><span class="o">++</span>
|
<a id="__codelineno-111-7" name="__codelineno-111-7" href="#__codelineno-111-7"></a><span class="w"> </span><span class="nx">count</span><span class="o">++</span>
|
||||||
|
@ -5318,7 +5343,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
<a id="__codelineno-112-2" name="__codelineno-112-2" href="#__codelineno-112-2"></a><span class="kd">function</span><span class="w"> </span><span class="nx">exponential</span><span class="p">(</span><span class="nx">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-112-2" name="__codelineno-112-2" href="#__codelineno-112-2"></a><span class="kd">function</span><span class="w"> </span><span class="nx">exponential</span><span class="p">(</span><span class="nx">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-112-3" name="__codelineno-112-3" href="#__codelineno-112-3"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
|
<a id="__codelineno-112-3" name="__codelineno-112-3" href="#__codelineno-112-3"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
|
||||||
<a id="__codelineno-112-4" name="__codelineno-112-4" href="#__codelineno-112-4"></a><span class="w"> </span><span class="nx">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1</span><span class="p">;</span>
|
<a id="__codelineno-112-4" name="__codelineno-112-4" href="#__codelineno-112-4"></a><span class="w"> </span><span class="nx">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1</span><span class="p">;</span>
|
||||||
<a id="__codelineno-112-5" name="__codelineno-112-5" href="#__codelineno-112-5"></a><span class="w"> </span><span class="c1">// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
<a id="__codelineno-112-5" name="__codelineno-112-5" href="#__codelineno-112-5"></a><span class="w"> </span><span class="c1">// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
||||||
<a id="__codelineno-112-6" name="__codelineno-112-6" href="#__codelineno-112-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-112-6" name="__codelineno-112-6" href="#__codelineno-112-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-112-7" name="__codelineno-112-7" href="#__codelineno-112-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">base</span><span class="p">;</span><span class="w"> </span><span class="nx">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-112-7" name="__codelineno-112-7" href="#__codelineno-112-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">base</span><span class="p">;</span><span class="w"> </span><span class="nx">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-112-8" name="__codelineno-112-8" href="#__codelineno-112-8"></a><span class="w"> </span><span class="nx">count</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-112-8" name="__codelineno-112-8" href="#__codelineno-112-8"></a><span class="w"> </span><span class="nx">count</span><span class="o">++</span><span class="p">;</span>
|
||||||
|
@ -5335,7 +5360,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
<a id="__codelineno-113-2" name="__codelineno-113-2" href="#__codelineno-113-2"></a><span class="kd">function</span><span class="w"> </span><span class="nx">exponential</span><span class="p">(</span><span class="nx">n</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-113-2" name="__codelineno-113-2" href="#__codelineno-113-2"></a><span class="kd">function</span><span class="w"> </span><span class="nx">exponential</span><span class="p">(</span><span class="nx">n</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-113-3" name="__codelineno-113-3" href="#__codelineno-113-3"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
|
<a id="__codelineno-113-3" name="__codelineno-113-3" href="#__codelineno-113-3"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nx">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
|
||||||
<a id="__codelineno-113-4" name="__codelineno-113-4" href="#__codelineno-113-4"></a><span class="w"> </span><span class="nx">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1</span><span class="p">;</span>
|
<a id="__codelineno-113-4" name="__codelineno-113-4" href="#__codelineno-113-4"></a><span class="w"> </span><span class="nx">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1</span><span class="p">;</span>
|
||||||
<a id="__codelineno-113-5" name="__codelineno-113-5" href="#__codelineno-113-5"></a><span class="w"> </span><span class="c1">// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
<a id="__codelineno-113-5" name="__codelineno-113-5" href="#__codelineno-113-5"></a><span class="w"> </span><span class="c1">// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
||||||
<a id="__codelineno-113-6" name="__codelineno-113-6" href="#__codelineno-113-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-113-6" name="__codelineno-113-6" href="#__codelineno-113-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-113-7" name="__codelineno-113-7" href="#__codelineno-113-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">base</span><span class="p">;</span><span class="w"> </span><span class="nx">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-113-7" name="__codelineno-113-7" href="#__codelineno-113-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">base</span><span class="p">;</span><span class="w"> </span><span class="nx">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-113-8" name="__codelineno-113-8" href="#__codelineno-113-8"></a><span class="w"> </span><span class="nx">count</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-113-8" name="__codelineno-113-8" href="#__codelineno-113-8"></a><span class="w"> </span><span class="nx">count</span><span class="o">++</span><span class="p">;</span>
|
||||||
|
@ -5352,7 +5377,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
<a id="__codelineno-114-2" name="__codelineno-114-2" href="#__codelineno-114-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">exponential</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-114-2" name="__codelineno-114-2" href="#__codelineno-114-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">exponential</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-114-3" name="__codelineno-114-3" href="#__codelineno-114-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-114-3" name="__codelineno-114-3" href="#__codelineno-114-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-114-4" name="__codelineno-114-4" href="#__codelineno-114-4"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">bas</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
|
<a id="__codelineno-114-4" name="__codelineno-114-4" href="#__codelineno-114-4"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">bas</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
|
||||||
<a id="__codelineno-114-5" name="__codelineno-114-5" href="#__codelineno-114-5"></a><span class="w"> </span><span class="c1">// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
<a id="__codelineno-114-5" name="__codelineno-114-5" href="#__codelineno-114-5"></a><span class="w"> </span><span class="c1">// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
||||||
<a id="__codelineno-114-6" name="__codelineno-114-6" href="#__codelineno-114-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-114-6" name="__codelineno-114-6" href="#__codelineno-114-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">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="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-114-7" name="__codelineno-114-7" href="#__codelineno-114-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">j</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="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">bas</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-114-7" name="__codelineno-114-7" href="#__codelineno-114-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">j</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="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">bas</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-114-8" name="__codelineno-114-8" href="#__codelineno-114-8"></a><span class="w"> </span><span class="n">count</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-114-8" name="__codelineno-114-8" href="#__codelineno-114-8"></a><span class="w"> </span><span class="n">count</span><span class="o">++</span><span class="p">;</span>
|
||||||
|
@ -5368,7 +5393,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
<div class="highlight"><span class="filename">time_complexity.cs</span><pre><span></span><code><a id="__codelineno-115-1" name="__codelineno-115-1" href="#__codelineno-115-1"></a><span class="cm">/* 指数阶(循环实现) */</span>
|
<div class="highlight"><span class="filename">time_complexity.cs</span><pre><span></span><code><a id="__codelineno-115-1" name="__codelineno-115-1" href="#__codelineno-115-1"></a><span class="cm">/* 指数阶(循环实现) */</span>
|
||||||
<a id="__codelineno-115-2" name="__codelineno-115-2" href="#__codelineno-115-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">exponential</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-115-2" name="__codelineno-115-2" href="#__codelineno-115-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">exponential</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-115-3" name="__codelineno-115-3" href="#__codelineno-115-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="n">bas</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">;</span>
|
<a id="__codelineno-115-3" name="__codelineno-115-3" href="#__codelineno-115-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="n">bas</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">;</span>
|
||||||
<a id="__codelineno-115-4" name="__codelineno-115-4" href="#__codelineno-115-4"></a><span class="w"> </span><span class="c1">// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
<a id="__codelineno-115-4" name="__codelineno-115-4" href="#__codelineno-115-4"></a><span class="w"> </span><span class="c1">// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
||||||
<a id="__codelineno-115-5" name="__codelineno-115-5" href="#__codelineno-115-5"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-115-5" name="__codelineno-115-5" href="#__codelineno-115-5"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-115-6" name="__codelineno-115-6" href="#__codelineno-115-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">bas</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-115-6" name="__codelineno-115-6" href="#__codelineno-115-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">bas</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-115-7" name="__codelineno-115-7" href="#__codelineno-115-7"></a><span class="w"> </span><span class="n">count</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-115-7" name="__codelineno-115-7" href="#__codelineno-115-7"></a><span class="w"> </span><span class="n">count</span><span class="o">++</span><span class="p">;</span>
|
||||||
|
@ -5385,7 +5410,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
<a id="__codelineno-116-2" name="__codelineno-116-2" href="#__codelineno-116-2"></a><span class="kd">func</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="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
<a id="__codelineno-116-2" name="__codelineno-116-2" href="#__codelineno-116-2"></a><span class="kd">func</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="p">-></span> <span class="nb">Int</span> <span class="p">{</span>
|
||||||
<a id="__codelineno-116-3" name="__codelineno-116-3" href="#__codelineno-116-3"></a> <span class="kd">var</span> <span class="nv">count</span> <span class="p">=</span> <span class="mi">0</span>
|
<a id="__codelineno-116-3" name="__codelineno-116-3" href="#__codelineno-116-3"></a> <span class="kd">var</span> <span class="nv">count</span> <span class="p">=</span> <span class="mi">0</span>
|
||||||
<a id="__codelineno-116-4" name="__codelineno-116-4" href="#__codelineno-116-4"></a> <span class="kd">var</span> <span class="nv">base</span> <span class="p">=</span> <span class="mi">1</span>
|
<a id="__codelineno-116-4" name="__codelineno-116-4" href="#__codelineno-116-4"></a> <span class="kd">var</span> <span class="nv">base</span> <span class="p">=</span> <span class="mi">1</span>
|
||||||
<a id="__codelineno-116-5" name="__codelineno-116-5" href="#__codelineno-116-5"></a> <span class="c1">// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
<a id="__codelineno-116-5" name="__codelineno-116-5" href="#__codelineno-116-5"></a> <span class="c1">// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
||||||
<a id="__codelineno-116-6" name="__codelineno-116-6" href="#__codelineno-116-6"></a> <span class="k">for</span> <span class="kc">_</span> <span class="k">in</span> <span class="mi">0</span> <span class="p">..</span><span class="o"><</span> <span class="n">n</span> <span class="p">{</span>
|
<a id="__codelineno-116-6" name="__codelineno-116-6" href="#__codelineno-116-6"></a> <span class="k">for</span> <span class="kc">_</span> <span class="k">in</span> <span class="mi">0</span> <span class="p">..</span><span class="o"><</span> <span class="n">n</span> <span class="p">{</span>
|
||||||
<a id="__codelineno-116-7" name="__codelineno-116-7" href="#__codelineno-116-7"></a> <span class="k">for</span> <span class="kc">_</span> <span class="k">in</span> <span class="mi">0</span> <span class="p">..</span><span class="o"><</span> <span class="n">base</span> <span class="p">{</span>
|
<a id="__codelineno-116-7" name="__codelineno-116-7" href="#__codelineno-116-7"></a> <span class="k">for</span> <span class="kc">_</span> <span class="k">in</span> <span class="mi">0</span> <span class="p">..</span><span class="o"><</span> <span class="n">base</span> <span class="p">{</span>
|
||||||
<a id="__codelineno-116-8" name="__codelineno-116-8" href="#__codelineno-116-8"></a> <span class="bp">count</span> <span class="o">+=</span> <span class="mi">1</span>
|
<a id="__codelineno-116-8" name="__codelineno-116-8" href="#__codelineno-116-8"></a> <span class="bp">count</span> <span class="o">+=</span> <span class="mi">1</span>
|
||||||
|
@ -5403,7 +5428,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
<a id="__codelineno-117-3" name="__codelineno-117-3" href="#__codelineno-117-3"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">count</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-117-3" name="__codelineno-117-3" href="#__codelineno-117-3"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">count</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-117-4" name="__codelineno-117-4" href="#__codelineno-117-4"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">bas</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
|
<a id="__codelineno-117-4" name="__codelineno-117-4" href="#__codelineno-117-4"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">bas</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
|
||||||
<a id="__codelineno-117-5" name="__codelineno-117-5" href="#__codelineno-117-5"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">i</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-117-5" name="__codelineno-117-5" href="#__codelineno-117-5"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">i</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-117-6" name="__codelineno-117-6" href="#__codelineno-117-6"></a><span class="w"> </span><span class="c1">// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
<a id="__codelineno-117-6" name="__codelineno-117-6" href="#__codelineno-117-6"></a><span class="w"> </span><span class="c1">// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
||||||
<a id="__codelineno-117-7" name="__codelineno-117-7" href="#__codelineno-117-7"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="p">(</span><span class="n">i</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="p">{</span>
|
<a id="__codelineno-117-7" name="__codelineno-117-7" href="#__codelineno-117-7"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="p">(</span><span class="n">i</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="p">{</span>
|
||||||
<a id="__codelineno-117-8" name="__codelineno-117-8" href="#__codelineno-117-8"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">j</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-117-8" name="__codelineno-117-8" href="#__codelineno-117-8"></a><span class="w"> </span><span class="kr">var</span><span class="w"> </span><span class="n">j</span><span class="o">:</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-117-9" name="__codelineno-117-9" href="#__codelineno-117-9"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">bas</span><span class="p">)</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="p">(</span><span class="n">j</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="p">{</span>
|
<a id="__codelineno-117-9" name="__codelineno-117-9" href="#__codelineno-117-9"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">bas</span><span class="p">)</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="p">(</span><span class="n">j</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="p">{</span>
|
||||||
|
@ -5420,7 +5445,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
<div class="highlight"><span class="filename">time_complexity.dart</span><pre><span></span><code><a id="__codelineno-118-1" name="__codelineno-118-1" href="#__codelineno-118-1"></a><span class="cm">/* 指数阶(循环实现) */</span>
|
<div class="highlight"><span class="filename">time_complexity.dart</span><pre><span></span><code><a id="__codelineno-118-1" name="__codelineno-118-1" href="#__codelineno-118-1"></a><span class="cm">/* 指数阶(循环实现) */</span>
|
||||||
<a id="__codelineno-118-2" name="__codelineno-118-2" href="#__codelineno-118-2"></a><span class="kt">int</span><span class="w"> </span><span class="n">exponential</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-118-2" name="__codelineno-118-2" href="#__codelineno-118-2"></a><span class="kt">int</span><span class="w"> </span><span class="n">exponential</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-118-3" name="__codelineno-118-3" href="#__codelineno-118-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="n">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">;</span>
|
<a id="__codelineno-118-3" name="__codelineno-118-3" href="#__codelineno-118-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="n">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">;</span>
|
||||||
<a id="__codelineno-118-4" name="__codelineno-118-4" href="#__codelineno-118-4"></a><span class="w"> </span><span class="c1">// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
<a id="__codelineno-118-4" name="__codelineno-118-4" href="#__codelineno-118-4"></a><span class="w"> </span><span class="c1">// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
||||||
<a id="__codelineno-118-5" name="__codelineno-118-5" href="#__codelineno-118-5"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-118-5" name="__codelineno-118-5" href="#__codelineno-118-5"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-118-6" name="__codelineno-118-6" href="#__codelineno-118-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">base</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-118-6" name="__codelineno-118-6" href="#__codelineno-118-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">base</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-118-7" name="__codelineno-118-7" href="#__codelineno-118-7"></a><span class="w"> </span><span class="n">count</span><span class="o">++</span><span class="p">;</span>
|
<a id="__codelineno-118-7" name="__codelineno-118-7" href="#__codelineno-118-7"></a><span class="w"> </span><span class="n">count</span><span class="o">++</span><span class="p">;</span>
|
||||||
|
@ -5437,7 +5462,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
<a id="__codelineno-119-2" name="__codelineno-119-2" href="#__codelineno-119-2"></a><span class="k">fn</span> <span class="nf">exponential</span><span class="p">(</span><span class="n">n</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span>-> <span class="kt">i32</span> <span class="p">{</span>
|
<a id="__codelineno-119-2" name="__codelineno-119-2" href="#__codelineno-119-2"></a><span class="k">fn</span> <span class="nf">exponential</span><span class="p">(</span><span class="n">n</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span>-> <span class="kt">i32</span> <span class="p">{</span>
|
||||||
<a id="__codelineno-119-3" name="__codelineno-119-3" href="#__codelineno-119-3"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
<a id="__codelineno-119-3" name="__codelineno-119-3" href="#__codelineno-119-3"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
|
||||||
<a id="__codelineno-119-4" name="__codelineno-119-4" href="#__codelineno-119-4"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
|
<a id="__codelineno-119-4" name="__codelineno-119-4" href="#__codelineno-119-4"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
|
||||||
<a id="__codelineno-119-5" name="__codelineno-119-5" href="#__codelineno-119-5"></a><span class="w"> </span><span class="c1">// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
<a id="__codelineno-119-5" name="__codelineno-119-5" href="#__codelineno-119-5"></a><span class="w"> </span><span class="c1">// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)</span>
|
||||||
<a id="__codelineno-119-6" name="__codelineno-119-6" href="#__codelineno-119-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="n">n</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-119-6" name="__codelineno-119-6" href="#__codelineno-119-6"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="n">n</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-119-7" name="__codelineno-119-7" href="#__codelineno-119-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="n">base</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-119-7" name="__codelineno-119-7" href="#__codelineno-119-7"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="n">base</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-119-8" name="__codelineno-119-8" href="#__codelineno-119-8"></a><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span>
|
<a id="__codelineno-119-8" name="__codelineno-119-8" href="#__codelineno-119-8"></a><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span>
|
||||||
|
@ -5454,7 +5479,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
<p><img alt="指数阶的时间复杂度" src="../time_complexity.assets/time_complexity_exponential.png" /></p>
|
<p><img alt="指数阶的时间复杂度" src="../time_complexity.assets/time_complexity_exponential.png" /></p>
|
||||||
<p align="center"> Fig. 指数阶的时间复杂度 </p>
|
<p align="center"> Fig. 指数阶的时间复杂度 </p>
|
||||||
|
|
||||||
<p>在实际算法中,指数阶常出现于递归函数。例如以下代码,不断地一分为二,经过 <span class="arithmatex">\(n\)</span> 次分裂后停止。</p>
|
<p>在实际算法中,指数阶常出现于递归函数。例如以下代码,其递归地一分为二,经过 <span class="arithmatex">\(n\)</span> 次分裂后停止。</p>
|
||||||
<div class="tabbed-set tabbed-alternate" data-tabs="11:12"><input checked="checked" id="__tabbed_11_1" name="__tabbed_11" type="radio" /><input id="__tabbed_11_2" name="__tabbed_11" type="radio" /><input id="__tabbed_11_3" name="__tabbed_11" type="radio" /><input id="__tabbed_11_4" name="__tabbed_11" type="radio" /><input id="__tabbed_11_5" name="__tabbed_11" type="radio" /><input id="__tabbed_11_6" name="__tabbed_11" type="radio" /><input id="__tabbed_11_7" name="__tabbed_11" type="radio" /><input id="__tabbed_11_8" name="__tabbed_11" type="radio" /><input id="__tabbed_11_9" name="__tabbed_11" type="radio" /><input id="__tabbed_11_10" name="__tabbed_11" type="radio" /><input id="__tabbed_11_11" name="__tabbed_11" type="radio" /><input id="__tabbed_11_12" name="__tabbed_11" type="radio" /><div class="tabbed-labels"><label for="__tabbed_11_1">Java</label><label for="__tabbed_11_2">C++</label><label for="__tabbed_11_3">Python</label><label for="__tabbed_11_4">Go</label><label for="__tabbed_11_5">JS</label><label for="__tabbed_11_6">TS</label><label for="__tabbed_11_7">C</label><label for="__tabbed_11_8">C#</label><label for="__tabbed_11_9">Swift</label><label for="__tabbed_11_10">Zig</label><label for="__tabbed_11_11">Dart</label><label for="__tabbed_11_12">Rust</label></div>
|
<div class="tabbed-set tabbed-alternate" data-tabs="11:12"><input checked="checked" id="__tabbed_11_1" name="__tabbed_11" type="radio" /><input id="__tabbed_11_2" name="__tabbed_11" type="radio" /><input id="__tabbed_11_3" name="__tabbed_11" type="radio" /><input id="__tabbed_11_4" name="__tabbed_11" type="radio" /><input id="__tabbed_11_5" name="__tabbed_11" type="radio" /><input id="__tabbed_11_6" name="__tabbed_11" type="radio" /><input id="__tabbed_11_7" name="__tabbed_11" type="radio" /><input id="__tabbed_11_8" name="__tabbed_11" type="radio" /><input id="__tabbed_11_9" name="__tabbed_11" type="radio" /><input id="__tabbed_11_10" name="__tabbed_11" type="radio" /><input id="__tabbed_11_11" name="__tabbed_11" type="radio" /><input id="__tabbed_11_12" name="__tabbed_11" type="radio" /><div class="tabbed-labels"><label for="__tabbed_11_1">Java</label><label for="__tabbed_11_2">C++</label><label for="__tabbed_11_3">Python</label><label for="__tabbed_11_4">Go</label><label for="__tabbed_11_5">JS</label><label for="__tabbed_11_6">TS</label><label for="__tabbed_11_7">C</label><label for="__tabbed_11_8">C#</label><label for="__tabbed_11_9">Swift</label><label for="__tabbed_11_10">Zig</label><label for="__tabbed_11_11">Dart</label><label for="__tabbed_11_12">Rust</label></div>
|
||||||
<div class="tabbed-content">
|
<div class="tabbed-content">
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -5564,10 +5589,9 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<p>指数阶增长非常迅速,在穷举法(暴力搜索、回溯等)中比较常见。对于数据规模较大的问题,指数阶是不可接受的,通常需要使用「动态规划」或「贪心」等算法来解决。</p>
|
||||||
<h3 id="olog-n">对数阶 <span class="arithmatex">\(O(\log n)\)</span><a class="headerlink" href="#olog-n" title="Permanent link">¶</a></h3>
|
<h3 id="olog-n">对数阶 <span class="arithmatex">\(O(\log n)\)</span><a class="headerlink" href="#olog-n" title="Permanent link">¶</a></h3>
|
||||||
<p>与指数阶相反,对数阶反映了“每轮缩减到一半的情况”。对数阶仅次于常数阶,时间增长缓慢,是理想的时间复杂度。</p>
|
<p>与指数阶相反,对数阶反映了“每轮缩减到一半”的情况。设输入数据大小为 <span class="arithmatex">\(n\)</span> ,由于每轮缩减到一半,因此循环次数是 <span class="arithmatex">\(\log_2 n\)</span> ,即 <span class="arithmatex">\(2^n\)</span> 的反函数。</p>
|
||||||
<p>对数阶常出现于「二分查找」和「分治算法」中,体现了“一分为多”和“化繁为简”的算法思想。</p>
|
|
||||||
<p>设输入数据大小为 <span class="arithmatex">\(n\)</span> ,由于每轮缩减到一半,因此循环次数是 <span class="arithmatex">\(\log_2 n\)</span> ,即 <span class="arithmatex">\(2^n\)</span> 的反函数。</p>
|
|
||||||
<div class="tabbed-set tabbed-alternate" data-tabs="12:12"><input checked="checked" id="__tabbed_12_1" name="__tabbed_12" type="radio" /><input id="__tabbed_12_2" name="__tabbed_12" type="radio" /><input id="__tabbed_12_3" name="__tabbed_12" type="radio" /><input id="__tabbed_12_4" name="__tabbed_12" type="radio" /><input id="__tabbed_12_5" name="__tabbed_12" type="radio" /><input id="__tabbed_12_6" name="__tabbed_12" type="radio" /><input id="__tabbed_12_7" name="__tabbed_12" type="radio" /><input id="__tabbed_12_8" name="__tabbed_12" type="radio" /><input id="__tabbed_12_9" name="__tabbed_12" type="radio" /><input id="__tabbed_12_10" name="__tabbed_12" type="radio" /><input id="__tabbed_12_11" name="__tabbed_12" type="radio" /><input id="__tabbed_12_12" name="__tabbed_12" type="radio" /><div class="tabbed-labels"><label for="__tabbed_12_1">Java</label><label for="__tabbed_12_2">C++</label><label for="__tabbed_12_3">Python</label><label for="__tabbed_12_4">Go</label><label for="__tabbed_12_5">JS</label><label for="__tabbed_12_6">TS</label><label for="__tabbed_12_7">C</label><label for="__tabbed_12_8">C#</label><label for="__tabbed_12_9">Swift</label><label for="__tabbed_12_10">Zig</label><label for="__tabbed_12_11">Dart</label><label for="__tabbed_12_12">Rust</label></div>
|
<div class="tabbed-set tabbed-alternate" data-tabs="12:12"><input checked="checked" id="__tabbed_12_1" name="__tabbed_12" type="radio" /><input id="__tabbed_12_2" name="__tabbed_12" type="radio" /><input id="__tabbed_12_3" name="__tabbed_12" type="radio" /><input id="__tabbed_12_4" name="__tabbed_12" type="radio" /><input id="__tabbed_12_5" name="__tabbed_12" type="radio" /><input id="__tabbed_12_6" name="__tabbed_12" type="radio" /><input id="__tabbed_12_7" name="__tabbed_12" type="radio" /><input id="__tabbed_12_8" name="__tabbed_12" type="radio" /><input id="__tabbed_12_9" name="__tabbed_12" type="radio" /><input id="__tabbed_12_10" name="__tabbed_12" type="radio" /><input id="__tabbed_12_11" name="__tabbed_12" type="radio" /><input id="__tabbed_12_12" name="__tabbed_12" type="radio" /><div class="tabbed-labels"><label for="__tabbed_12_1">Java</label><label for="__tabbed_12_2">C++</label><label for="__tabbed_12_3">Python</label><label for="__tabbed_12_4">Go</label><label for="__tabbed_12_5">JS</label><label for="__tabbed_12_6">TS</label><label for="__tabbed_12_7">C</label><label for="__tabbed_12_8">C#</label><label for="__tabbed_12_9">Swift</label><label for="__tabbed_12_10">Zig</label><label for="__tabbed_12_11">Dart</label><label for="__tabbed_12_12">Rust</label></div>
|
||||||
<div class="tabbed-content">
|
<div class="tabbed-content">
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -5830,6 +5854,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<p>对数阶常出现于基于「分治」的算法中,体现了“一分为多”和“化繁为简”的算法思想。它增长缓慢,是理想的时间复杂度,仅次于常数阶。</p>
|
||||||
<h3 id="on-log-n">线性对数阶 <span class="arithmatex">\(O(n \log n)\)</span><a class="headerlink" href="#on-log-n" title="Permanent link">¶</a></h3>
|
<h3 id="on-log-n">线性对数阶 <span class="arithmatex">\(O(n \log n)\)</span><a class="headerlink" href="#on-log-n" title="Permanent link">¶</a></h3>
|
||||||
<p>线性对数阶常出现于嵌套循环中,两层循环的时间复杂度分别为 <span class="arithmatex">\(O(\log n)\)</span> 和 <span class="arithmatex">\(O(n)\)</span> 。</p>
|
<p>线性对数阶常出现于嵌套循环中,两层循环的时间复杂度分别为 <span class="arithmatex">\(O(\log n)\)</span> 和 <span class="arithmatex">\(O(n)\)</span> 。</p>
|
||||||
<p>主流排序算法的时间复杂度通常为 <span class="arithmatex">\(O(n \log n)\)</span> ,例如快速排序、归并排序、堆排序等。</p>
|
<p>主流排序算法的时间复杂度通常为 <span class="arithmatex">\(O(n \log n)\)</span> ,例如快速排序、归并排序、堆排序等。</p>
|
||||||
|
@ -5999,7 +6024,7 @@ O((n - 1) \frac{n}{2}) = O(n^2)
|
||||||
<p align="center"> Fig. 线性对数阶的时间复杂度 </p>
|
<p align="center"> Fig. 线性对数阶的时间复杂度 </p>
|
||||||
|
|
||||||
<h3 id="on_1">阶乘阶 <span class="arithmatex">\(O(n!)\)</span><a class="headerlink" href="#on_1" title="Permanent link">¶</a></h3>
|
<h3 id="on_1">阶乘阶 <span class="arithmatex">\(O(n!)\)</span><a class="headerlink" href="#on_1" title="Permanent link">¶</a></h3>
|
||||||
<p>阶乘阶对应数学上的「全排列」问题。给定 <span class="arithmatex">\(n\)</span> 个互不重复的元素,求其所有可能的排列方案,方案数量为:</p>
|
<p>阶乘阶对应数学上的“全排列”问题。给定 <span class="arithmatex">\(n\)</span> 个互不重复的元素,求其所有可能的排列方案,方案数量为:</p>
|
||||||
<div class="arithmatex">\[
|
<div class="arithmatex">\[
|
||||||
n! = n \times (n - 1) \times (n - 2) \times \cdots \times 2 \times 1
|
n! = n \times (n - 1) \times (n - 2) \times \cdots \times 2 \times 1
|
||||||
\]</div>
|
\]</div>
|
||||||
|
@ -6175,13 +6200,14 @@ n! = n \times (n - 1) \times (n - 2) \times \cdots \times 2 \times 1
|
||||||
<p><img alt="阶乘阶的时间复杂度" src="../time_complexity.assets/time_complexity_factorial.png" /></p>
|
<p><img alt="阶乘阶的时间复杂度" src="../time_complexity.assets/time_complexity_factorial.png" /></p>
|
||||||
<p align="center"> Fig. 阶乘阶的时间复杂度 </p>
|
<p align="center"> Fig. 阶乘阶的时间复杂度 </p>
|
||||||
|
|
||||||
<h2 id="226">2.2.6. 最差、最佳、平均时间复杂度<a class="headerlink" href="#226" title="Permanent link">¶</a></h2>
|
<p>请注意,因为 <span class="arithmatex">\(n! > 2^n\)</span> ,所以阶乘阶比指数阶增长地更快,在 <span class="arithmatex">\(n\)</span> 较大时也是不可接受的。</p>
|
||||||
<p><strong>某些算法的时间复杂度不是固定的,而是与输入数据的分布有关</strong>。例如,假设输入一个长度为 <span class="arithmatex">\(n\)</span> 的数组 <code>nums</code> ,其中 <code>nums</code> 由从 <span class="arithmatex">\(1\)</span> 至 <span class="arithmatex">\(n\)</span> 的数字组成,但元素顺序是随机打乱的;算法的任务是返回元素 <span class="arithmatex">\(1\)</span> 的索引。我们可以得出以下结论:</p>
|
<h2 id="225">2.2.5. 最差、最佳、平均时间复杂度<a class="headerlink" href="#225" title="Permanent link">¶</a></h2>
|
||||||
|
<p><strong>算法的时间效率往往不是固定的,而是与输入数据的分布有关</strong>。假设输入一个长度为 <span class="arithmatex">\(n\)</span> 的数组 <code>nums</code> ,其中 <code>nums</code> 由从 <span class="arithmatex">\(1\)</span> 至 <span class="arithmatex">\(n\)</span> 的数字组成,但元素顺序是随机打乱的,任务目标是返回元素 <span class="arithmatex">\(1\)</span> 的索引。我们可以得出以下结论:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li>当 <code>nums = [?, ?, ..., 1]</code> ,即当末尾元素是 <span class="arithmatex">\(1\)</span> 时,需要完整遍历数组,此时达到 <strong>最差时间复杂度 <span class="arithmatex">\(O(n)\)</span></strong> 。</li>
|
<li>当 <code>nums = [?, ?, ..., 1]</code> ,即当末尾元素是 <span class="arithmatex">\(1\)</span> 时,需要完整遍历数组,<strong>达到最差时间复杂度 <span class="arithmatex">\(O(n)\)</span></strong> 。</li>
|
||||||
<li>当 <code>nums = [1, ?, ?, ...]</code> ,即当首个数字为 <span class="arithmatex">\(1\)</span> 时,无论数组多长都不需要继续遍历,此时达到 <strong>最佳时间复杂度 <span class="arithmatex">\(\Omega(1)\)</span></strong> 。</li>
|
<li>当 <code>nums = [1, ?, ?, ...]</code> ,即当首个数字为 <span class="arithmatex">\(1\)</span> 时,无论数组多长都不需要继续遍历,<strong>达到最佳时间复杂度 <span class="arithmatex">\(\Omega(1)\)</span></strong> 。</li>
|
||||||
</ul>
|
</ul>
|
||||||
<p>“函数渐近上界”使用大 <span class="arithmatex">\(O\)</span> 记号表示,代表「最差时间复杂度」。相应地,“函数渐近下界”用 <span class="arithmatex">\(\Omega\)</span> 记号来表示,代表「最佳时间复杂度」。</p>
|
<p>「最差时间复杂度」对应函数渐近上界,使用大 <span class="arithmatex">\(O\)</span> 记号表示。相应地,「最佳时间复杂度」对应函数渐近下界,用 <span class="arithmatex">\(\Omega\)</span> 记号表示。</p>
|
||||||
<div class="tabbed-set tabbed-alternate" data-tabs="16:12"><input checked="checked" id="__tabbed_16_1" name="__tabbed_16" type="radio" /><input id="__tabbed_16_2" name="__tabbed_16" type="radio" /><input id="__tabbed_16_3" name="__tabbed_16" type="radio" /><input id="__tabbed_16_4" name="__tabbed_16" type="radio" /><input id="__tabbed_16_5" name="__tabbed_16" type="radio" /><input id="__tabbed_16_6" name="__tabbed_16" type="radio" /><input id="__tabbed_16_7" name="__tabbed_16" type="radio" /><input id="__tabbed_16_8" name="__tabbed_16" type="radio" /><input id="__tabbed_16_9" name="__tabbed_16" type="radio" /><input id="__tabbed_16_10" name="__tabbed_16" type="radio" /><input id="__tabbed_16_11" name="__tabbed_16" type="radio" /><input id="__tabbed_16_12" name="__tabbed_16" type="radio" /><div class="tabbed-labels"><label for="__tabbed_16_1">Java</label><label for="__tabbed_16_2">C++</label><label for="__tabbed_16_3">Python</label><label for="__tabbed_16_4">Go</label><label for="__tabbed_16_5">JS</label><label for="__tabbed_16_6">TS</label><label for="__tabbed_16_7">C</label><label for="__tabbed_16_8">C#</label><label for="__tabbed_16_9">Swift</label><label for="__tabbed_16_10">Zig</label><label for="__tabbed_16_11">Dart</label><label for="__tabbed_16_12">Rust</label></div>
|
<div class="tabbed-set tabbed-alternate" data-tabs="16:12"><input checked="checked" id="__tabbed_16_1" name="__tabbed_16" type="radio" /><input id="__tabbed_16_2" name="__tabbed_16" type="radio" /><input id="__tabbed_16_3" name="__tabbed_16" type="radio" /><input id="__tabbed_16_4" name="__tabbed_16" type="radio" /><input id="__tabbed_16_5" name="__tabbed_16" type="radio" /><input id="__tabbed_16_6" name="__tabbed_16" type="radio" /><input id="__tabbed_16_7" name="__tabbed_16" type="radio" /><input id="__tabbed_16_8" name="__tabbed_16" type="radio" /><input id="__tabbed_16_9" name="__tabbed_16" type="radio" /><input id="__tabbed_16_10" name="__tabbed_16" type="radio" /><input id="__tabbed_16_11" name="__tabbed_16" type="radio" /><input id="__tabbed_16_12" name="__tabbed_16" type="radio" /><div class="tabbed-labels"><label for="__tabbed_16_1">Java</label><label for="__tabbed_16_2">C++</label><label for="__tabbed_16_3">Python</label><label for="__tabbed_16_4">Go</label><label for="__tabbed_16_5">JS</label><label for="__tabbed_16_6">TS</label><label for="__tabbed_16_7">C</label><label for="__tabbed_16_8">C#</label><label for="__tabbed_16_9">Swift</label><label for="__tabbed_16_10">Zig</label><label for="__tabbed_16_11">Dart</label><label for="__tabbed_16_12">Rust</label></div>
|
||||||
<div class="tabbed-content">
|
<div class="tabbed-content">
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -6512,13 +6538,10 @@ n! = n \times (n - 1) \times (n - 2) \times \cdots \times 2 \times 1
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="admonition tip">
|
<p>值得说明的是,我们在实际中很少使用「最佳时间复杂度」,因为通常只有在很小概率下才能达到,可能会带来一定的误导性。<strong>而「最差时间复杂度」更为实用,因为它给出了一个效率安全值</strong>,让我们可以放心地使用算法。</p>
|
||||||
<p class="admonition-title">Tip</p>
|
<p>从上述示例可以看出,最差或最佳时间复杂度只出现于“特殊的数据分布”,这些情况的出现概率可能很小,并不能真实地反映算法运行效率。相比之下,<strong>「平均时间复杂度」可以体现算法在随机输入数据下的运行效率</strong>,用 <span class="arithmatex">\(\Theta\)</span> 记号来表示。</p>
|
||||||
<p>实际应用中我们很少使用「最佳时间复杂度」,因为通常只有在很小概率下才能达到,可能会带来一定的误导性。相反,「最差时间复杂度」更为实用,因为它给出了一个“效率安全值”,让我们可以放心地使用算法。</p>
|
|
||||||
</div>
|
|
||||||
<p>从上述示例可以看出,最差或最佳时间复杂度只出现在“特殊分布的数据”中,这些情况的出现概率可能很小,因此并不能最真实地反映算法运行效率。相较之下,<strong>「平均时间复杂度」可以体现算法在随机输入数据下的运行效率</strong>,用 <span class="arithmatex">\(\Theta\)</span> 记号来表示。</p>
|
|
||||||
<p>对于部分算法,我们可以简单地推算出随机数据分布下的平均情况。比如上述示例,由于输入数组是被打乱的,因此元素 <span class="arithmatex">\(1\)</span> 出现在任意索引的概率都是相等的,那么算法的平均循环次数则是数组长度的一半 <span class="arithmatex">\(\frac{n}{2}\)</span> ,平均时间复杂度为 <span class="arithmatex">\(\Theta(\frac{n}{2}) = \Theta(n)\)</span> 。</p>
|
<p>对于部分算法,我们可以简单地推算出随机数据分布下的平均情况。比如上述示例,由于输入数组是被打乱的,因此元素 <span class="arithmatex">\(1\)</span> 出现在任意索引的概率都是相等的,那么算法的平均循环次数则是数组长度的一半 <span class="arithmatex">\(\frac{n}{2}\)</span> ,平均时间复杂度为 <span class="arithmatex">\(\Theta(\frac{n}{2}) = \Theta(n)\)</span> 。</p>
|
||||||
<p>但在实际应用中,尤其是较为复杂的算法,计算平均时间复杂度比较困难,因为很难简便地分析出在数据分布下的整体数学期望。在这种情况下,我们通常使用最差时间复杂度作为算法效率的评判标准。</p>
|
<p>但对于较为复杂的算法,计算平均时间复杂度往往是比较困难的,因为很难分析出在数据分布下的整体数学期望。在这种情况下,我们通常使用最差时间复杂度作为算法效率的评判标准。</p>
|
||||||
<div class="admonition question">
|
<div class="admonition question">
|
||||||
<p class="admonition-title">为什么很少看到 <span class="arithmatex">\(\Theta\)</span> 符号?</p>
|
<p class="admonition-title">为什么很少看到 <span class="arithmatex">\(\Theta\)</span> 符号?</p>
|
||||||
<p>可能由于 <span class="arithmatex">\(O\)</span> 符号过于朗朗上口,我们常常使用它来表示「平均复杂度」,但从严格意义上看,这种做法并不规范。在本书和其他资料中,若遇到类似“平均时间复杂度 <span class="arithmatex">\(O(n)\)</span>”的表述,请将其直接理解为 <span class="arithmatex">\(\Theta(n)\)</span> 。</p>
|
<p>可能由于 <span class="arithmatex">\(O\)</span> 符号过于朗朗上口,我们常常使用它来表示「平均复杂度」,但从严格意义上看,这种做法并不规范。在本书和其他资料中,若遇到类似“平均时间复杂度 <span class="arithmatex">\(O(n)\)</span>”的表述,请将其直接理解为 <span class="arithmatex">\(\Theta(n)\)</span> 。</p>
|
||||||
|
|
|
@ -3426,7 +3426,7 @@
|
||||||
|
|
||||||
|
|
||||||
<h1 id="61">6.1. 哈希表<a class="headerlink" href="#61" title="Permanent link">¶</a></h1>
|
<h1 id="61">6.1. 哈希表<a class="headerlink" href="#61" title="Permanent link">¶</a></h1>
|
||||||
<p>「哈希表 Hash Table」通过建立键 <code>key</code> 与值 <code>value</code> 之间的映射,实现高效的元素查询。具体而言,我们向哈希表输入一个 <code>key</code> ,则可以在 <span class="arithmatex">\(O(1)\)</span> 时间内获取对应的 <code>value</code> 。</p>
|
<p>散列表,又称「哈希表 Hash Table」,其通过建立键 <code>key</code> 与值 <code>value</code> 之间的映射,实现高效的元素查询。具体而言,我们向哈希表输入一个键 <code>key</code> ,则可以在 <span class="arithmatex">\(O(1)\)</span> 时间内获取对应的值 <code>value</code> 。</p>
|
||||||
<p>以一个包含 <span class="arithmatex">\(n\)</span> 个学生的数据库为例,每个学生都有“姓名”和“学号”两项数据。假如我们希望实现“输入一个学号,返回对应的姓名”的查询功能,则可以采用哈希表来实现。</p>
|
<p>以一个包含 <span class="arithmatex">\(n\)</span> 个学生的数据库为例,每个学生都有“姓名”和“学号”两项数据。假如我们希望实现“输入一个学号,返回对应的姓名”的查询功能,则可以采用哈希表来实现。</p>
|
||||||
<p><img alt="哈希表的抽象表示" src="../hash_map.assets/hash_table_lookup.png" /></p>
|
<p><img alt="哈希表的抽象表示" src="../hash_map.assets/hash_table_lookup.png" /></p>
|
||||||
<p align="center"> Fig. 哈希表的抽象表示 </p>
|
<p align="center"> Fig. 哈希表的抽象表示 </p>
|
||||||
|
@ -3518,44 +3518,44 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">hash_map.py</span><pre><span></span><code><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="c1"># 初始化哈希表</span>
|
<div class="highlight"><span class="filename">hash_map.py</span><pre><span></span><code><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="c1"># 初始化哈希表</span>
|
||||||
<a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="n">mapp</span><span class="p">:</span> <span class="n">Dict</span> <span class="o">=</span> <span class="p">{}</span>
|
<a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="n">hmap</span><span class="p">:</span> <span class="n">Dict</span> <span class="o">=</span> <span class="p">{}</span>
|
||||||
<a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a>
|
<a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a>
|
||||||
<a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="c1"># 添加操作</span>
|
<a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="c1"># 添加操作</span>
|
||||||
<a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="c1"># 在哈希表中添加键值对 (key, value)</span>
|
<a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="c1"># 在哈希表中添加键值对 (key, value)</span>
|
||||||
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="n">mapp</span><span class="p">[</span><span class="mi">12836</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"小哈"</span>
|
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="n">hmap</span><span class="p">[</span><span class="mi">12836</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"小哈"</span>
|
||||||
<a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a><span class="n">mapp</span><span class="p">[</span><span class="mi">15937</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"小啰"</span>
|
<a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a><span class="n">hmap</span><span class="p">[</span><span class="mi">15937</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"小啰"</span>
|
||||||
<a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a><span class="n">mapp</span><span class="p">[</span><span class="mi">16750</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"小算"</span>
|
<a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a><span class="n">hmap</span><span class="p">[</span><span class="mi">16750</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"小算"</span>
|
||||||
<a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a><span class="n">mapp</span><span class="p">[</span><span class="mi">13276</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"小法"</span>
|
<a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a><span class="n">hmap</span><span class="p">[</span><span class="mi">13276</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"小法"</span>
|
||||||
<a id="__codelineno-2-10" name="__codelineno-2-10" href="#__codelineno-2-10"></a><span class="n">mapp</span><span class="p">[</span><span class="mi">10583</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"小鸭"</span>
|
<a id="__codelineno-2-10" name="__codelineno-2-10" href="#__codelineno-2-10"></a><span class="n">hmap</span><span class="p">[</span><span class="mi">10583</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"小鸭"</span>
|
||||||
<a id="__codelineno-2-11" name="__codelineno-2-11" href="#__codelineno-2-11"></a>
|
<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="c1"># 查询操作</span>
|
<a id="__codelineno-2-12" name="__codelineno-2-12" href="#__codelineno-2-12"></a><span class="c1"># 查询操作</span>
|
||||||
<a id="__codelineno-2-13" name="__codelineno-2-13" href="#__codelineno-2-13"></a><span class="c1"># 向哈希表输入键 key ,得到值 value</span>
|
<a id="__codelineno-2-13" name="__codelineno-2-13" href="#__codelineno-2-13"></a><span class="c1"># 向哈希表输入键 key ,得到值 value</span>
|
||||||
<a id="__codelineno-2-14" name="__codelineno-2-14" href="#__codelineno-2-14"></a><span class="n">name</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="n">mapp</span><span class="p">[</span><span class="mi">15937</span><span class="p">]</span>
|
<a id="__codelineno-2-14" name="__codelineno-2-14" href="#__codelineno-2-14"></a><span class="n">name</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="n">hmap</span><span class="p">[</span><span class="mi">15937</span><span class="p">]</span>
|
||||||
<a id="__codelineno-2-15" name="__codelineno-2-15" href="#__codelineno-2-15"></a>
|
<a id="__codelineno-2-15" name="__codelineno-2-15" href="#__codelineno-2-15"></a>
|
||||||
<a id="__codelineno-2-16" name="__codelineno-2-16" href="#__codelineno-2-16"></a><span class="c1"># 删除操作</span>
|
<a id="__codelineno-2-16" name="__codelineno-2-16" href="#__codelineno-2-16"></a><span class="c1"># 删除操作</span>
|
||||||
<a id="__codelineno-2-17" name="__codelineno-2-17" href="#__codelineno-2-17"></a><span class="c1"># 在哈希表中删除键值对 (key, value)</span>
|
<a id="__codelineno-2-17" name="__codelineno-2-17" href="#__codelineno-2-17"></a><span class="c1"># 在哈希表中删除键值对 (key, value)</span>
|
||||||
<a id="__codelineno-2-18" name="__codelineno-2-18" href="#__codelineno-2-18"></a><span class="n">mapp</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">10583</span><span class="p">)</span>
|
<a id="__codelineno-2-18" name="__codelineno-2-18" href="#__codelineno-2-18"></a><span class="n">hmap</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">10583</span><span class="p">)</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">hash_map.go</span><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="cm">/* 初始化哈希表 */</span>
|
<div class="highlight"><span class="filename">hash_map.go</span><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="cm">/* 初始化哈希表 */</span>
|
||||||
<a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a><span class="nx">mapp</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="kt">int</span><span class="p">]</span><span class="kt">string</span><span class="p">)</span>
|
<a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a><span class="nx">hmap</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="kt">int</span><span class="p">]</span><span class="kt">string</span><span class="p">)</span>
|
||||||
<a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a>
|
<a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a>
|
||||||
<a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="cm">/* 添加操作 */</span>
|
<a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="cm">/* 添加操作 */</span>
|
||||||
<a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="c1">// 在哈希表中添加键值对 (key, value)</span>
|
<a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="c1">// 在哈希表中添加键值对 (key, value)</span>
|
||||||
<a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a><span class="nx">mapp</span><span class="p">[</span><span class="mi">12836</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="s">"小哈"</span>
|
<a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a><span class="nx">hmap</span><span class="p">[</span><span class="mi">12836</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="s">"小哈"</span>
|
||||||
<a id="__codelineno-3-7" name="__codelineno-3-7" href="#__codelineno-3-7"></a><span class="nx">mapp</span><span class="p">[</span><span class="mi">15937</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="s">"小啰"</span>
|
<a id="__codelineno-3-7" name="__codelineno-3-7" href="#__codelineno-3-7"></a><span class="nx">hmap</span><span class="p">[</span><span class="mi">15937</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="s">"小啰"</span>
|
||||||
<a id="__codelineno-3-8" name="__codelineno-3-8" href="#__codelineno-3-8"></a><span class="nx">mapp</span><span class="p">[</span><span class="mi">16750</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="s">"小算"</span>
|
<a id="__codelineno-3-8" name="__codelineno-3-8" href="#__codelineno-3-8"></a><span class="nx">hmap</span><span class="p">[</span><span class="mi">16750</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="s">"小算"</span>
|
||||||
<a id="__codelineno-3-9" name="__codelineno-3-9" href="#__codelineno-3-9"></a><span class="nx">mapp</span><span class="p">[</span><span class="mi">13276</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="s">"小法"</span>
|
<a id="__codelineno-3-9" name="__codelineno-3-9" href="#__codelineno-3-9"></a><span class="nx">hmap</span><span class="p">[</span><span class="mi">13276</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="s">"小法"</span>
|
||||||
<a id="__codelineno-3-10" name="__codelineno-3-10" href="#__codelineno-3-10"></a><span class="nx">mapp</span><span class="p">[</span><span class="mi">10583</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="s">"小鸭"</span>
|
<a id="__codelineno-3-10" name="__codelineno-3-10" href="#__codelineno-3-10"></a><span class="nx">hmap</span><span class="p">[</span><span class="mi">10583</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="s">"小鸭"</span>
|
||||||
<a id="__codelineno-3-11" name="__codelineno-3-11" href="#__codelineno-3-11"></a>
|
<a id="__codelineno-3-11" name="__codelineno-3-11" href="#__codelineno-3-11"></a>
|
||||||
<a id="__codelineno-3-12" name="__codelineno-3-12" href="#__codelineno-3-12"></a><span class="cm">/* 查询操作 */</span>
|
<a id="__codelineno-3-12" name="__codelineno-3-12" href="#__codelineno-3-12"></a><span class="cm">/* 查询操作 */</span>
|
||||||
<a id="__codelineno-3-13" name="__codelineno-3-13" href="#__codelineno-3-13"></a><span class="c1">// 向哈希表输入键 key ,得到值 value</span>
|
<a id="__codelineno-3-13" name="__codelineno-3-13" href="#__codelineno-3-13"></a><span class="c1">// 向哈希表输入键 key ,得到值 value</span>
|
||||||
<a id="__codelineno-3-14" name="__codelineno-3-14" href="#__codelineno-3-14"></a><span class="nx">name</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">mapp</span><span class="p">[</span><span class="mi">15937</span><span class="p">]</span>
|
<a id="__codelineno-3-14" name="__codelineno-3-14" href="#__codelineno-3-14"></a><span class="nx">name</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">hmap</span><span class="p">[</span><span class="mi">15937</span><span class="p">]</span>
|
||||||
<a id="__codelineno-3-15" name="__codelineno-3-15" href="#__codelineno-3-15"></a>
|
<a id="__codelineno-3-15" name="__codelineno-3-15" href="#__codelineno-3-15"></a>
|
||||||
<a id="__codelineno-3-16" name="__codelineno-3-16" href="#__codelineno-3-16"></a><span class="cm">/* 删除操作 */</span>
|
<a id="__codelineno-3-16" name="__codelineno-3-16" href="#__codelineno-3-16"></a><span class="cm">/* 删除操作 */</span>
|
||||||
<a id="__codelineno-3-17" name="__codelineno-3-17" href="#__codelineno-3-17"></a><span class="c1">// 在哈希表中删除键值对 (key, value)</span>
|
<a id="__codelineno-3-17" name="__codelineno-3-17" href="#__codelineno-3-17"></a><span class="c1">// 在哈希表中删除键值对 (key, value)</span>
|
||||||
<a id="__codelineno-3-18" name="__codelineno-3-18" href="#__codelineno-3-18"></a><span class="nb">delete</span><span class="p">(</span><span class="nx">mapp</span><span class="p">,</span><span class="w"> </span><span class="mi">10583</span><span class="p">)</span>
|
<a id="__codelineno-3-18" name="__codelineno-3-18" href="#__codelineno-3-18"></a><span class="nb">delete</span><span class="p">(</span><span class="nx">hmap</span><span class="p">,</span><span class="w"> </span><span class="mi">10583</span><span class="p">)</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
|
@ -3718,28 +3718,28 @@
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">hash_map.py</span><pre><span></span><code><a id="__codelineno-14-1" name="__codelineno-14-1" href="#__codelineno-14-1"></a><span class="c1"># 遍历哈希表</span>
|
<div class="highlight"><span class="filename">hash_map.py</span><pre><span></span><code><a id="__codelineno-14-1" name="__codelineno-14-1" href="#__codelineno-14-1"></a><span class="c1"># 遍历哈希表</span>
|
||||||
<a id="__codelineno-14-2" name="__codelineno-14-2" href="#__codelineno-14-2"></a><span class="c1"># 遍历键值对 key->value</span>
|
<a id="__codelineno-14-2" name="__codelineno-14-2" href="#__codelineno-14-2"></a><span class="c1"># 遍历键值对 key->value</span>
|
||||||
<a id="__codelineno-14-3" name="__codelineno-14-3" href="#__codelineno-14-3"></a><span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">mapp</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
|
<a id="__codelineno-14-3" name="__codelineno-14-3" href="#__codelineno-14-3"></a><span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">hmap</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
|
||||||
<a id="__codelineno-14-4" name="__codelineno-14-4" href="#__codelineno-14-4"></a> <span class="nb">print</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="s2">"->"</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
|
<a id="__codelineno-14-4" name="__codelineno-14-4" href="#__codelineno-14-4"></a> <span class="nb">print</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="s2">"->"</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
|
||||||
<a id="__codelineno-14-5" name="__codelineno-14-5" href="#__codelineno-14-5"></a><span class="c1"># 单独遍历键 key</span>
|
<a id="__codelineno-14-5" name="__codelineno-14-5" href="#__codelineno-14-5"></a><span class="c1"># 单独遍历键 key</span>
|
||||||
<a id="__codelineno-14-6" name="__codelineno-14-6" href="#__codelineno-14-6"></a><span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">mapp</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
|
<a id="__codelineno-14-6" name="__codelineno-14-6" href="#__codelineno-14-6"></a><span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">hmap</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
|
||||||
<a id="__codelineno-14-7" name="__codelineno-14-7" href="#__codelineno-14-7"></a> <span class="nb">print</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
<a id="__codelineno-14-7" name="__codelineno-14-7" href="#__codelineno-14-7"></a> <span class="nb">print</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
||||||
<a id="__codelineno-14-8" name="__codelineno-14-8" href="#__codelineno-14-8"></a><span class="c1"># 单独遍历值 value</span>
|
<a id="__codelineno-14-8" name="__codelineno-14-8" href="#__codelineno-14-8"></a><span class="c1"># 单独遍历值 value</span>
|
||||||
<a id="__codelineno-14-9" name="__codelineno-14-9" href="#__codelineno-14-9"></a><span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">mapp</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
|
<a id="__codelineno-14-9" name="__codelineno-14-9" href="#__codelineno-14-9"></a><span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">hmap</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
|
||||||
<a id="__codelineno-14-10" name="__codelineno-14-10" href="#__codelineno-14-10"></a> <span class="nb">print</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
|
<a id="__codelineno-14-10" name="__codelineno-14-10" href="#__codelineno-14-10"></a> <span class="nb">print</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tabbed-block">
|
<div class="tabbed-block">
|
||||||
<div class="highlight"><span class="filename">hash_map_test.go</span><pre><span></span><code><a id="__codelineno-15-1" name="__codelineno-15-1" href="#__codelineno-15-1"></a><span class="cm">/* 遍历哈希表 */</span>
|
<div class="highlight"><span class="filename">hash_map_test.go</span><pre><span></span><code><a id="__codelineno-15-1" name="__codelineno-15-1" href="#__codelineno-15-1"></a><span class="cm">/* 遍历哈希表 */</span>
|
||||||
<a id="__codelineno-15-2" name="__codelineno-15-2" href="#__codelineno-15-2"></a><span class="c1">// 遍历键值对 key->value</span>
|
<a id="__codelineno-15-2" name="__codelineno-15-2" href="#__codelineno-15-2"></a><span class="c1">// 遍历键值对 key->value</span>
|
||||||
<a id="__codelineno-15-3" name="__codelineno-15-3" href="#__codelineno-15-3"></a><span class="k">for</span><span class="w"> </span><span class="nx">key</span><span class="p">,</span><span class="w"> </span><span class="nx">value</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">mapp</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-15-3" name="__codelineno-15-3" href="#__codelineno-15-3"></a><span class="k">for</span><span class="w"> </span><span class="nx">key</span><span class="p">,</span><span class="w"> </span><span class="nx">value</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">hmap</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-15-4" name="__codelineno-15-4" href="#__codelineno-15-4"></a><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span><span class="w"> </span><span class="s">"->"</span><span class="p">,</span><span class="w"> </span><span class="nx">value</span><span class="p">)</span>
|
<a id="__codelineno-15-4" name="__codelineno-15-4" href="#__codelineno-15-4"></a><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span><span class="w"> </span><span class="s">"->"</span><span class="p">,</span><span class="w"> </span><span class="nx">value</span><span class="p">)</span>
|
||||||
<a id="__codelineno-15-5" name="__codelineno-15-5" href="#__codelineno-15-5"></a><span class="p">}</span>
|
<a id="__codelineno-15-5" name="__codelineno-15-5" href="#__codelineno-15-5"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-15-6" name="__codelineno-15-6" href="#__codelineno-15-6"></a><span class="c1">// 单独遍历键 key</span>
|
<a id="__codelineno-15-6" name="__codelineno-15-6" href="#__codelineno-15-6"></a><span class="c1">// 单独遍历键 key</span>
|
||||||
<a id="__codelineno-15-7" name="__codelineno-15-7" href="#__codelineno-15-7"></a><span class="k">for</span><span class="w"> </span><span class="nx">key</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">mapp</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-15-7" name="__codelineno-15-7" href="#__codelineno-15-7"></a><span class="k">for</span><span class="w"> </span><span class="nx">key</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">hmap</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-15-8" name="__codelineno-15-8" href="#__codelineno-15-8"></a><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span>
|
<a id="__codelineno-15-8" name="__codelineno-15-8" href="#__codelineno-15-8"></a><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span>
|
||||||
<a id="__codelineno-15-9" name="__codelineno-15-9" href="#__codelineno-15-9"></a><span class="p">}</span>
|
<a id="__codelineno-15-9" name="__codelineno-15-9" href="#__codelineno-15-9"></a><span class="p">}</span>
|
||||||
<a id="__codelineno-15-10" name="__codelineno-15-10" href="#__codelineno-15-10"></a><span class="c1">// 单独遍历值 value</span>
|
<a id="__codelineno-15-10" name="__codelineno-15-10" href="#__codelineno-15-10"></a><span class="c1">// 单独遍历值 value</span>
|
||||||
<a id="__codelineno-15-11" name="__codelineno-15-11" href="#__codelineno-15-11"></a><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">value</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">mapp</span><span class="w"> </span><span class="p">{</span>
|
<a id="__codelineno-15-11" name="__codelineno-15-11" href="#__codelineno-15-11"></a><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">value</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">hmap</span><span class="w"> </span><span class="p">{</span>
|
||||||
<a id="__codelineno-15-12" name="__codelineno-15-12" href="#__codelineno-15-12"></a><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span>
|
<a id="__codelineno-15-12" name="__codelineno-15-12" href="#__codelineno-15-12"></a><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span>
|
||||||
<a id="__codelineno-15-13" name="__codelineno-15-13" href="#__codelineno-15-13"></a><span class="p">}</span>
|
<a id="__codelineno-15-13" name="__codelineno-15-13" href="#__codelineno-15-13"></a><span class="p">}</span>
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
|
|
|
@ -3377,14 +3377,13 @@
|
||||||
<p><strong>例二:整理扑克</strong>。我们在打牌时,每局都需要整理扑克牌,使其从小到大排列,实现流程如下:</p>
|
<p><strong>例二:整理扑克</strong>。我们在打牌时,每局都需要整理扑克牌,使其从小到大排列,实现流程如下:</p>
|
||||||
<ol>
|
<ol>
|
||||||
<li>将扑克牌划分为“有序”和“无序”两部分,并假设初始状态下最左 1 张扑克牌已经有序。</li>
|
<li>将扑克牌划分为“有序”和“无序”两部分,并假设初始状态下最左 1 张扑克牌已经有序。</li>
|
||||||
<li>在无序区间抽出一张扑克牌,插入至有序区间的正确位置;完成后最左 2 张扑克已经有序。</li>
|
<li>在无序部分抽出一张扑克牌,插入至有序部分的正确位置;完成后最左 2 张扑克已经有序。</li>
|
||||||
<li>在无序区间抽出一张扑克牌,插入至有序区间的正确位置;完成后最左 3 张扑克已经有序。</li>
|
<li>不断循环步骤 <code>2.</code> ,每一轮将一张扑克牌从无序部分插入至有序部分,直至所有扑克牌都有序。</li>
|
||||||
<li>不断循环以上操作,直至所有扑克牌都有序后终止。</li>
|
|
||||||
</ol>
|
</ol>
|
||||||
<p>以上整理扑克牌的方法本质上就是「插入排序」算法,它在处理小型数据集时非常高效。许多编程语言的排序库函数中都存在插入排序的身影。</p>
|
|
||||||
<p><img alt="扑克排序步骤" src="../algorithms_are_everywhere.assets/playing_cards_sorting.png" /></p>
|
<p><img alt="扑克排序步骤" src="../algorithms_are_everywhere.assets/playing_cards_sorting.png" /></p>
|
||||||
<p align="center"> Fig. 扑克排序步骤 </p>
|
<p align="center"> Fig. 扑克排序步骤 </p>
|
||||||
|
|
||||||
|
<p>上述整理扑克牌的方法本质上是「插入排序」算法,它在处理小型数据集时非常高效。许多编程语言的排序库函数中都存在插入排序的身影。</p>
|
||||||
<p><strong>例三:货币找零</strong>。假设我们在超市购买了 <span class="arithmatex">\(69\)</span> 元的商品,给收银员付了 <span class="arithmatex">\(100\)</span> 元,则收银员需要给我们找 <span class="arithmatex">\(31\)</span> 元。他会很自然地完成以下思考:</p>
|
<p><strong>例三:货币找零</strong>。假设我们在超市购买了 <span class="arithmatex">\(69\)</span> 元的商品,给收银员付了 <span class="arithmatex">\(100\)</span> 元,则收银员需要给我们找 <span class="arithmatex">\(31\)</span> 元。他会很自然地完成以下思考:</p>
|
||||||
<ol>
|
<ol>
|
||||||
<li>可选项是比 <span class="arithmatex">\(31\)</span> 元面值更小的货币,包括 <span class="arithmatex">\(1\)</span> , <span class="arithmatex">\(5\)</span> , <span class="arithmatex">\(10\)</span> , <span class="arithmatex">\(20\)</span> 元。</li>
|
<li>可选项是比 <span class="arithmatex">\(31\)</span> 元面值更小的货币,包括 <span class="arithmatex">\(1\)</span> , <span class="arithmatex">\(5\)</span> , <span class="arithmatex">\(10\)</span> , <span class="arithmatex">\(20\)</span> 元。</li>
|
||||||
|
@ -3393,14 +3392,14 @@
|
||||||
<li>从剩余可选项中拿出最大的 <span class="arithmatex">\(1\)</span> 元,剩余 <span class="arithmatex">\(1 - 1 = 0\)</span> 元。</li>
|
<li>从剩余可选项中拿出最大的 <span class="arithmatex">\(1\)</span> 元,剩余 <span class="arithmatex">\(1 - 1 = 0\)</span> 元。</li>
|
||||||
<li>完成找零,方案为 <span class="arithmatex">\(20 + 10 + 1 = 31\)</span> 元。</li>
|
<li>完成找零,方案为 <span class="arithmatex">\(20 + 10 + 1 = 31\)</span> 元。</li>
|
||||||
</ol>
|
</ol>
|
||||||
<p>在以上步骤中,我们每一步都采取当前看来最好的选择(尽可能用大面额的货币),最终得到了可行的找零方案。从数据结构与算法的角度看,这种方法本质上是「贪心算法」。</p>
|
|
||||||
<p><img alt="货币找零过程" src="../algorithms_are_everywhere.assets/greedy_change.png" /></p>
|
<p><img alt="货币找零过程" src="../algorithms_are_everywhere.assets/greedy_change.png" /></p>
|
||||||
<p align="center"> Fig. 货币找零过程 </p>
|
<p align="center"> Fig. 货币找零过程 </p>
|
||||||
|
|
||||||
|
<p>在以上步骤中,我们每一步都采取当前看来最好的选择(尽可能用大面额的货币),最终得到了可行的找零方案。从数据结构与算法的角度看,这种方法本质上是「贪心算法」。</p>
|
||||||
<p>小到烹饪一道菜,大到星际航行,几乎所有问题的解决都离不开算法。计算机的出现使我们能够通过编程将数据结构存储在内存中,同时编写代码调用 CPU 和 GPU 执行算法。这样一来,我们就能把生活中的问题转移到计算机上,以更高效的方式解决各种复杂问题。</p>
|
<p>小到烹饪一道菜,大到星际航行,几乎所有问题的解决都离不开算法。计算机的出现使我们能够通过编程将数据结构存储在内存中,同时编写代码调用 CPU 和 GPU 执行算法。这样一来,我们就能把生活中的问题转移到计算机上,以更高效的方式解决各种复杂问题。</p>
|
||||||
<div class="admonition tip">
|
<div class="admonition tip">
|
||||||
<p class="admonition-title">Tip</p>
|
<p class="admonition-title">Tip</p>
|
||||||
<p>阅读至此,如果你对数据结构、算法、数组和二分查找等概念仍感到一知半解,那么太好了!因为这正是本书存在的意义。接下来,这本书将一步步引导你深入数据结构与算法的知识殿堂。</p>
|
<p>阅读至此,如果你对数据结构、算法、数组和二分查找等概念仍感到一知半解,那么太好了!因为这正是本书存在的意义。接下来,这本书将引导你一步步深入数据结构与算法的知识殿堂。</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3427,22 +3427,26 @@
|
||||||
|
|
||||||
<h1 id="12">1.2. 算法是什么<a class="headerlink" href="#12" title="Permanent link">¶</a></h1>
|
<h1 id="12">1.2. 算法是什么<a class="headerlink" href="#12" title="Permanent link">¶</a></h1>
|
||||||
<h2 id="121">1.2.1. 算法定义<a class="headerlink" href="#121" title="Permanent link">¶</a></h2>
|
<h2 id="121">1.2.1. 算法定义<a class="headerlink" href="#121" title="Permanent link">¶</a></h2>
|
||||||
<p>「算法 Algorithm」是在有限时间内解决特定问题的一组指令或操作步骤。算法具有以下特性:</p>
|
<p>「算法 Algorithm」是在有限时间内解决特定问题的一组指令或操作步骤。它具有以下特性:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li>问题是明确的,包含清晰的输入和输出定义。</li>
|
<li>问题是明确的,包含清晰的输入和输出定义。</li>
|
||||||
<li>具有可行性,能够在有限步骤、时间和内存空间下完成。</li>
|
<li>具有可行性,能够在有限步骤、时间和内存空间下完成。</li>
|
||||||
<li>各步骤都有确定的含义,相同的输入和运行条件下,输出始终相同。</li>
|
<li>各步骤都有确定的含义,相同的输入和运行条件下,输出始终相同。</li>
|
||||||
</ul>
|
</ul>
|
||||||
<h2 id="122">1.2.2. 数据结构定义<a class="headerlink" href="#122" title="Permanent link">¶</a></h2>
|
<h2 id="122">1.2.2. 数据结构定义<a class="headerlink" href="#122" title="Permanent link">¶</a></h2>
|
||||||
<p>「数据结构 Data Structure」是计算机中组织和存储数据的方式。为了提高数据存储和操作性能,数据结构的设计目标包括:</p>
|
<p>「数据结构 Data Structure」是计算机中组织和存储数据的方式。它的设计目标包括:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li>空间占用尽量减少,节省计算机内存。</li>
|
<li>空间占用尽量减少,节省计算机内存。</li>
|
||||||
<li>数据操作尽可能快速,涵盖数据访问、添加、删除、更新等。</li>
|
<li>数据操作尽可能快速,涵盖数据访问、添加、删除、更新等。</li>
|
||||||
<li>提供简洁的数据表示和逻辑信息,以便使得算法高效运行。</li>
|
<li>提供简洁的数据表示和逻辑信息,以便使得算法高效运行。</li>
|
||||||
</ul>
|
</ul>
|
||||||
<p>数据结构设计是一个充满权衡的过程,这意味着要在某方面取得优势,往往需要在另一方面作出妥协。例如,链表相较于数组,在数据添加和删除操作上更加便捷,但牺牲了数据访问速度;图相较于链表,提供了更丰富的逻辑信息,但需要占用更大的内存空间。</p>
|
<p><strong>数据结构设计是一个充满权衡的过程</strong>。如果想要在某方面取得提升,往往需要在另一方面作出妥协,例如:</p>
|
||||||
|
<ul>
|
||||||
|
<li>链表相较于数组,在数据添加和删除操作上更加便捷,但牺牲了数据访问速度。</li>
|
||||||
|
<li>图相较于链表,提供了更丰富的逻辑信息,但需要占用更大的内存空间。</li>
|
||||||
|
</ul>
|
||||||
<h2 id="123">1.2.3. 数据结构与算法的关系<a class="headerlink" href="#123" title="Permanent link">¶</a></h2>
|
<h2 id="123">1.2.3. 数据结构与算法的关系<a class="headerlink" href="#123" title="Permanent link">¶</a></h2>
|
||||||
<p>「数据结构」与「算法」高度相关且紧密结合,具体表现在:</p>
|
<p>数据结构与算法高度相关、紧密结合,具体表现在:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li>数据结构是算法的基石。数据结构为算法提供了结构化存储的数据,以及用于操作数据的方法。</li>
|
<li>数据结构是算法的基石。数据结构为算法提供了结构化存储的数据,以及用于操作数据的方法。</li>
|
||||||
<li>算法是数据结构发挥的舞台。数据结构本身仅存储数据信息,通过结合算法才能解决特定问题。</li>
|
<li>算法是数据结构发挥的舞台。数据结构本身仅存储数据信息,通过结合算法才能解决特定问题。</li>
|
||||||
|
@ -3451,7 +3455,7 @@
|
||||||
<p><img alt="数据结构与算法的关系" src="../what_is_dsa.assets/relationship_between_data_structure_and_algorithm.png" /></p>
|
<p><img alt="数据结构与算法的关系" src="../what_is_dsa.assets/relationship_between_data_structure_and_algorithm.png" /></p>
|
||||||
<p align="center"> Fig. 数据结构与算法的关系 </p>
|
<p align="center"> Fig. 数据结构与算法的关系 </p>
|
||||||
|
|
||||||
<p>我们可以把数据结构与算法类比为拼装积木。一套积木,除了包含许多零件之外,还附有详细的组装说明书。我们按照说明书一步步操作,就能组装出精美的积木模型。</p>
|
<p>数据结构与算法犹如拼装积木。一套积木,除了包含许多零件之外,还附有详细的组装说明书。我们按照说明书一步步操作,就能组装出精美的积木模型。</p>
|
||||||
<p><img alt="拼装积木" src="../what_is_dsa.assets/assembling_blocks.jpg" /></p>
|
<p><img alt="拼装积木" src="../what_is_dsa.assets/assembling_blocks.jpg" /></p>
|
||||||
<p align="center"> Fig. 拼装积木 </p>
|
<p align="center"> Fig. 拼装积木 </p>
|
||||||
|
|
||||||
|
@ -3484,10 +3488,10 @@
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
<p>值得注意的是,数据结构与算法独立于编程语言。正因如此,本书得以提供多种编程语言的实现。</p>
|
<p>值得说明的是,数据结构与算法是独立于编程语言的。正因如此,本书得以提供多种编程语言的实现。</p>
|
||||||
<div class="admonition tip">
|
<div class="admonition tip">
|
||||||
<p class="admonition-title">约定俗成的简称</p>
|
<p class="admonition-title">约定俗成的简称</p>
|
||||||
<p>在实际讨论时,我们通常会将「数据结构与算法」简称为「算法」。例如,众所周知的 LeetCode 算法题目,实际上同时考察了数据结构和算法两方面的知识。</p>
|
<p>在实际讨论时,我们通常会将「数据结构与算法」简称为「算法」。比如众所周知的 LeetCode 算法题目,实际上同时考察了数据结构和算法两方面的知识。</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3595,14 +3595,15 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<h2 id="022">0.2.2. 在动画图解中高效学习<a class="headerlink" href="#022" title="Permanent link">¶</a></h2>
|
<h2 id="022">0.2.2. 在动画图解中高效学习<a class="headerlink" href="#022" title="Permanent link">¶</a></h2>
|
||||||
<p>相较于文字,视频和图片具有更高的信息密度和结构化程度,因此更易于理解。在本书中,<strong>重点和难点知识将主要通过动画和图解形式展示</strong>,而文字则作为动画和图片的解释与补充。</p>
|
<p>相较于文字,视频和图片具有更高的信息密度和结构化程度,更易于理解。在本书中,<strong>重点和难点知识将主要通过动画和图解形式展示</strong>,而文字则作为动画和图片的解释与补充。</p>
|
||||||
<p>在阅读本书时,如果发现某段内容提供了动画或图解,<strong>建议以图为主线</strong>,以文字(通常位于图像上方)为辅,综合两者来理解内容。</p>
|
<p>在阅读本书时,如果发现某段内容提供了动画或图解,<strong>建议以图为主线</strong>,以文字(通常位于图像上方)为辅,综合两者来理解内容。</p>
|
||||||
<p><img alt="动画图解示例" src="../../index.assets/animation.gif" /></p>
|
<p><img alt="动画图解示例" src="../../index.assets/animation.gif" /></p>
|
||||||
<p align="center"> Fig. 动画图解示例 </p>
|
<p align="center"> Fig. 动画图解示例 </p>
|
||||||
|
|
||||||
<h2 id="023">0.2.3. 在代码实践中加深理解<a class="headerlink" href="#023" title="Permanent link">¶</a></h2>
|
<h2 id="023">0.2.3. 在代码实践中加深理解<a class="headerlink" href="#023" title="Permanent link">¶</a></h2>
|
||||||
<p>本书的配套代码托管在<a href="https://github.com/krahets/hello-algo">GitHub 仓库</a>,<strong>源代码包含详细注释,并附有测试样例,可直接运行</strong>。</p>
|
<p>本书的配套代码被托管在 <a href="https://github.com/krahets/hello-algo">GitHub 仓库</a>。<strong>源代码附有测试样例,可一键运行</strong>。</p>
|
||||||
<p>如果学习时间有限,建议你至少通读并运行所有代码。如果时间充裕,<strong>建议参照代码自行敲一遍</strong>。与仅阅读代码相比,编写代码的过程往往能带来更多收获。</p>
|
<p>如果时间允许,<strong>建议你参照代码自行敲一遍</strong>。如果学习时间有限,请至少通读并运行所有代码。</p>
|
||||||
|
<p>与阅读代码相比,编写代码的过程往往能带来更多收获。<strong>动手学,才是真的学</strong>。</p>
|
||||||
<p><img alt="运行代码示例" src="../../index.assets/running_code.gif" /></p>
|
<p><img alt="运行代码示例" src="../../index.assets/running_code.gif" /></p>
|
||||||
<p align="center"> Fig. 运行代码示例 </p>
|
<p align="center"> Fig. 运行代码示例 </p>
|
||||||
|
|
||||||
|
|
File diff suppressed because one or more lines are too long
202
sitemap.xml
202
sitemap.xml
|
@ -2,507 +2,507 @@
|
||||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/</loc>
|
<loc>https://www.hello-algo.com/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_appendix/</loc>
|
<loc>https://www.hello-algo.com/chapter_appendix/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_appendix/contribution/</loc>
|
<loc>https://www.hello-algo.com/chapter_appendix/contribution/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_appendix/installation/</loc>
|
<loc>https://www.hello-algo.com/chapter_appendix/installation/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/</loc>
|
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/array/</loc>
|
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/array/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/linked_list/</loc>
|
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/linked_list/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/list/</loc>
|
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/list/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_array_and_linkedlist/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_backtracking/</loc>
|
<loc>https://www.hello-algo.com/chapter_backtracking/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_backtracking/backtracking_algorithm/</loc>
|
<loc>https://www.hello-algo.com/chapter_backtracking/backtracking_algorithm/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_backtracking/n_queens_problem/</loc>
|
<loc>https://www.hello-algo.com/chapter_backtracking/n_queens_problem/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_backtracking/permutations_problem/</loc>
|
<loc>https://www.hello-algo.com/chapter_backtracking/permutations_problem/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_backtracking/subset_sum_problem/</loc>
|
<loc>https://www.hello-algo.com/chapter_backtracking/subset_sum_problem/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_backtracking/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_backtracking/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/</loc>
|
<loc>https://www.hello-algo.com/chapter_computational_complexity/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/performance_evaluation/</loc>
|
<loc>https://www.hello-algo.com/chapter_computational_complexity/performance_evaluation/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/space_complexity/</loc>
|
<loc>https://www.hello-algo.com/chapter_computational_complexity/space_complexity/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_computational_complexity/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_computational_complexity/time_complexity/</loc>
|
<loc>https://www.hello-algo.com/chapter_computational_complexity/time_complexity/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_data_structure/</loc>
|
<loc>https://www.hello-algo.com/chapter_data_structure/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_data_structure/basic_data_types/</loc>
|
<loc>https://www.hello-algo.com/chapter_data_structure/basic_data_types/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_data_structure/character_encoding/</loc>
|
<loc>https://www.hello-algo.com/chapter_data_structure/character_encoding/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_data_structure/classification_of_data_structure/</loc>
|
<loc>https://www.hello-algo.com/chapter_data_structure/classification_of_data_structure/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_data_structure/number_encoding/</loc>
|
<loc>https://www.hello-algo.com/chapter_data_structure/number_encoding/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_data_structure/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_data_structure/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/</loc>
|
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/binary_search_recur/</loc>
|
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/binary_search_recur/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/build_binary_tree_problem/</loc>
|
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/build_binary_tree_problem/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/divide_and_conquer/</loc>
|
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/divide_and_conquer/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/hanota_problem/</loc>
|
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/hanota_problem/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_divide_and_conquer/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/</loc>
|
<loc>https://www.hello-algo.com/chapter_dynamic_programming/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/dp_problem_features/</loc>
|
<loc>https://www.hello-algo.com/chapter_dynamic_programming/dp_problem_features/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/dp_solution_pipeline/</loc>
|
<loc>https://www.hello-algo.com/chapter_dynamic_programming/dp_solution_pipeline/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/edit_distance_problem/</loc>
|
<loc>https://www.hello-algo.com/chapter_dynamic_programming/edit_distance_problem/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/intro_to_dynamic_programming/</loc>
|
<loc>https://www.hello-algo.com/chapter_dynamic_programming/intro_to_dynamic_programming/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/knapsack_problem/</loc>
|
<loc>https://www.hello-algo.com/chapter_dynamic_programming/knapsack_problem/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_dynamic_programming/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_dynamic_programming/unbounded_knapsack_problem/</loc>
|
<loc>https://www.hello-algo.com/chapter_dynamic_programming/unbounded_knapsack_problem/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_graph/</loc>
|
<loc>https://www.hello-algo.com/chapter_graph/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_graph/graph/</loc>
|
<loc>https://www.hello-algo.com/chapter_graph/graph/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_graph/graph_operations/</loc>
|
<loc>https://www.hello-algo.com/chapter_graph/graph_operations/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_graph/graph_traversal/</loc>
|
<loc>https://www.hello-algo.com/chapter_graph/graph_traversal/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_graph/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_graph/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_greedy/</loc>
|
<loc>https://www.hello-algo.com/chapter_greedy/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_greedy/fractional_knapsack_problem/</loc>
|
<loc>https://www.hello-algo.com/chapter_greedy/fractional_knapsack_problem/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_greedy/greedy_algorithm/</loc>
|
<loc>https://www.hello-algo.com/chapter_greedy/greedy_algorithm/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_greedy/max_capacity_problem/</loc>
|
<loc>https://www.hello-algo.com/chapter_greedy/max_capacity_problem/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_greedy/max_product_cutting_problem/</loc>
|
<loc>https://www.hello-algo.com/chapter_greedy/max_product_cutting_problem/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_greedy/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_greedy/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_hashing/</loc>
|
<loc>https://www.hello-algo.com/chapter_hashing/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_hashing/hash_algorithm/</loc>
|
<loc>https://www.hello-algo.com/chapter_hashing/hash_algorithm/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_hashing/hash_collision/</loc>
|
<loc>https://www.hello-algo.com/chapter_hashing/hash_collision/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_hashing/hash_map/</loc>
|
<loc>https://www.hello-algo.com/chapter_hashing/hash_map/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_hashing/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_hashing/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_heap/</loc>
|
<loc>https://www.hello-algo.com/chapter_heap/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_heap/build_heap/</loc>
|
<loc>https://www.hello-algo.com/chapter_heap/build_heap/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_heap/heap/</loc>
|
<loc>https://www.hello-algo.com/chapter_heap/heap/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_heap/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_heap/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_heap/top_k/</loc>
|
<loc>https://www.hello-algo.com/chapter_heap/top_k/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_introduction/</loc>
|
<loc>https://www.hello-algo.com/chapter_introduction/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_introduction/algorithms_are_everywhere/</loc>
|
<loc>https://www.hello-algo.com/chapter_introduction/algorithms_are_everywhere/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_introduction/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_introduction/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_introduction/what_is_dsa/</loc>
|
<loc>https://www.hello-algo.com/chapter_introduction/what_is_dsa/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_preface/</loc>
|
<loc>https://www.hello-algo.com/chapter_preface/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_preface/about_the_book/</loc>
|
<loc>https://www.hello-algo.com/chapter_preface/about_the_book/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_preface/suggestions/</loc>
|
<loc>https://www.hello-algo.com/chapter_preface/suggestions/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_preface/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_preface/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_reference/</loc>
|
<loc>https://www.hello-algo.com/chapter_reference/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_searching/</loc>
|
<loc>https://www.hello-algo.com/chapter_searching/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_searching/binary_search/</loc>
|
<loc>https://www.hello-algo.com/chapter_searching/binary_search/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_searching/binary_search_edge/</loc>
|
<loc>https://www.hello-algo.com/chapter_searching/binary_search_edge/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_searching/binary_search_insertion/</loc>
|
<loc>https://www.hello-algo.com/chapter_searching/binary_search_insertion/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_searching/replace_linear_by_hashing/</loc>
|
<loc>https://www.hello-algo.com/chapter_searching/replace_linear_by_hashing/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_searching/searching_algorithm_revisited/</loc>
|
<loc>https://www.hello-algo.com/chapter_searching/searching_algorithm_revisited/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_searching/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_searching/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_sorting/</loc>
|
<loc>https://www.hello-algo.com/chapter_sorting/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_sorting/bubble_sort/</loc>
|
<loc>https://www.hello-algo.com/chapter_sorting/bubble_sort/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_sorting/bucket_sort/</loc>
|
<loc>https://www.hello-algo.com/chapter_sorting/bucket_sort/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_sorting/counting_sort/</loc>
|
<loc>https://www.hello-algo.com/chapter_sorting/counting_sort/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_sorting/heap_sort/</loc>
|
<loc>https://www.hello-algo.com/chapter_sorting/heap_sort/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_sorting/insertion_sort/</loc>
|
<loc>https://www.hello-algo.com/chapter_sorting/insertion_sort/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_sorting/merge_sort/</loc>
|
<loc>https://www.hello-algo.com/chapter_sorting/merge_sort/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_sorting/quick_sort/</loc>
|
<loc>https://www.hello-algo.com/chapter_sorting/quick_sort/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_sorting/radix_sort/</loc>
|
<loc>https://www.hello-algo.com/chapter_sorting/radix_sort/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_sorting/selection_sort/</loc>
|
<loc>https://www.hello-algo.com/chapter_sorting/selection_sort/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_sorting/sorting_algorithm/</loc>
|
<loc>https://www.hello-algo.com/chapter_sorting/sorting_algorithm/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_sorting/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_sorting/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/</loc>
|
<loc>https://www.hello-algo.com/chapter_stack_and_queue/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/deque/</loc>
|
<loc>https://www.hello-algo.com/chapter_stack_and_queue/deque/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/queue/</loc>
|
<loc>https://www.hello-algo.com/chapter_stack_and_queue/queue/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/stack/</loc>
|
<loc>https://www.hello-algo.com/chapter_stack_and_queue/stack/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_stack_and_queue/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_stack_and_queue/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_tree/</loc>
|
<loc>https://www.hello-algo.com/chapter_tree/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_tree/array_representation_of_tree/</loc>
|
<loc>https://www.hello-algo.com/chapter_tree/array_representation_of_tree/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_tree/avl_tree/</loc>
|
<loc>https://www.hello-algo.com/chapter_tree/avl_tree/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_tree/binary_search_tree/</loc>
|
<loc>https://www.hello-algo.com/chapter_tree/binary_search_tree/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_tree/binary_tree/</loc>
|
<loc>https://www.hello-algo.com/chapter_tree/binary_tree/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_tree/binary_tree_traversal/</loc>
|
<loc>https://www.hello-algo.com/chapter_tree/binary_tree_traversal/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>https://www.hello-algo.com/chapter_tree/summary/</loc>
|
<loc>https://www.hello-algo.com/chapter_tree/summary/</loc>
|
||||||
<lastmod>2023-08-06</lastmod>
|
<lastmod>2023-08-08</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
</urlset>
|
</urlset>
|
BIN
sitemap.xml.gz
BIN
sitemap.xml.gz
Binary file not shown.
Loading…
Reference in a new issue